从命令行运行 Java 程序

2022-01-23 00:00:00 command java

所以我在这里有一个菜鸟时刻,我以前从未使用过命令行来运行 java 程序,但我现在需要.我遇到的问题是,当我尝试运行程序时,我得到了 ClassNotFoundException.我的课程叫做 OmadUpdate.我已经使用 javac 命令将 OmadUpdate.java 文件编译到 OmadUpdate.class 中.我检查了目录,它们肯定都在那里,但是当我运行 java OmadUpdate 命令时,它给了我一条错误消息,说

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

Exception in thread "main" java.lang.NoClassDefFoundError: OmadUpdate (wrong name:     org/openmetadata/main/OmadUpdate)
......
......
Could not find the main class: OmadUpdate. Program will exit

但它就在目录中.当我键入 dir 时,我同时拥有 OmadUpdate.class 和 OmadUpdate.java.我什至尝试过使用java org.openmetadata.main.OmadUpdate",因为这是它所在的包名.我难住了.感谢您的帮助.

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.

推荐答案

您的类似乎已在 org.openmetadata.main 包中声明.

Your class appears to have been declared in the org.openmetadata.main package.

要让java正确加载类,它需要位于与包结构匹配的正确目录结构中.

To get java to load the class correctly, it needs to be in the correct directory structure that matches the package structure.

所以 org.openmetadata.main.OmadUpdate 的类文件应该在目录 orgopenmetadatamain 中.

So the classfiles for org.openmetadata.main.OmadUpdate should be in the directory orgopenmetadatamain.

然后,当你运行 java 命令时,这个目录结构的根目录应该在类路径上 - 举个简单的例子,这只是意味着你的当前目录应该是 orgopenmetadatamain.

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 orgopenmetadatamain.

运行 java 时,您需要使用句点而不是斜杠指定完整的类名,即

When running java you need to specify the full classname using periods not slashes, i.e.

java org.openmetadata.main.OmadUpdate

相关文章