从命令行运行 Java 程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9879679/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Running Java Program From Command Line
提问by decal
so I am having a noob moment here, I haven't ever used the command line to run a java program before but I need to right now. The problem I am having is that when I try to run the program I get a ClassNotFoundException. My class is called OmadUpdate. I already have compiled the OmadUpdate.java file into OmadUpdate.class using the javac command. I have checked the directory and they are both definitely there, however when I run the java OmadUpdate command, it gives me an error message saying
所以我在这里有一个菜鸟时刻,我以前从未使用过命令行来运行 Java 程序,但我现在需要。我遇到的问题是,当我尝试运行该程序时,出现 ClassNotFoundException。我的课程叫做 OmadUpdate。我已经使用 javac 命令将 OmadUpdate.java 文件编译成 OmadUpdate.class。我已经检查了目录,它们肯定都在那里,但是当我运行 java OmadUpdate 命令时,它给了我一条错误消息说
Exception in thread "main" java.lang.NoClassDefFoundError: OmadUpdate (wrong name: org/openmetadata/main/OmadUpdate)
......
......
Could not find the main class: OmadUpdate. Program will exit
But its right there in the directory. When I type dir I have both OmadUpdate.class and OmadUpdate.java. I have even tried using "java org.openmetadata.main.OmadUpdate" because that is the package name that it is under. I am stumped. Thanks for the assistance.
但它就在目录中。当我输入 dir 时,我同时拥有 OmadUpdate.class 和 OmadUpdate.java。我什至尝试使用“java org.openmetadata.main.OmadUpdate”,因为这是它所在的包名。我难住了。感谢您的帮助。
回答by DNA
Your class appears to have been declared in the org.openmetadata.main
package.
您的类似乎已在org.openmetadata.main
包中声明。
To get java to load the class correctly, it needs to be in the correct directory structure that matches the package structure.
为了让java正确加载类,它需要在与包结构匹配的正确目录结构中。
So the classfiles for org.openmetadata.main.OmadUpdate
should be in the directory org\openmetadata\main
.
所以类文件org.openmetadata.main.OmadUpdate
应该在目录中org\openmetadata\main
。
Then, when you run the java
command, the root of this directory structure should be on the classpath - for a simple example this just means that your current directory should be the parent directory of org\openmetadata\main
.
然后,当您运行该java
命令时,此目录结构的根目录应该在类路径上 - 对于一个简单的示例,这仅意味着您的当前目录应该是 org\openmetadata\main
.
When running java
you need to specify the full classname using periods not slashes, i.e.
运行时,java
您需要使用句点而不是斜杠指定完整的类名,即
java org.openmetadata.main.OmadUpdate
回答by rob
After you compile the class with javac, you'll have the following directory structure:
使用 javac 编译该类后,您将拥有以下目录结构:
org/
openmetadata/
main/
OmadUpdate.class
OmadUpdate.java
Make sure you're in the parent directory of org, then run
确保您在 org 的父目录中,然后运行
java -cp . org.openmetadata.main.OmadUpdate
回答by Edwin Buck
Class names have their nested package names separated by periods, while the directories use slashes. Odds are good you tried java -cp . org/openmetadata/main/OmadUpdate
when you should have (since you are specifying a class name) tried java -cp . org.openmetadata.main.OmadUpdate
类名的嵌套包名用句点分隔,而目录使用斜杠。java -cp . org/openmetadata/main/OmadUpdate
当你应该尝试的时候(因为你指定了一个类名),你尝试过的可能性很大java -cp . org.openmetadata.main.OmadUpdate
Note that for this to work, you must run it in the directory just above the org
subdirectory. Otherwise that classpath directive cp .
will start the search in the wrong directory.
请注意,要使其工作,您必须在org
子目录正上方的目录中运行它。否则,该类路径指令cp .
将在错误的目录中开始搜索。
回答by Alex
launch your java app with the classpath set:
使用类路径集启动您的 Java 应用程序:
java -cp . org.openmetadata.main.OmadUpdate
回答by ControlAltDel
-cp . won't do anything I don't think. You have to make sure you are invoking java in the right directory, which is the part to the first package name / folder (in your case org)
-cp 。不会做我不认为的任何事情。您必须确保在正确的目录中调用 java,这是第一个包名称/文件夹的一部分(在您的情况下为 org)
You need to use the full package and class name to run it.
您需要使用完整的包和类名来运行它。