从命令行运行Java程序 [英] Running Java Program From Command Line
问题描述
所以我有一个noob的时刻在这里,我没有使用命令行运行一个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
package。
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
的类文件应该位于目录 org\openmetadata\main 。
So the classfiles for org.openmetadata.main.OmadUpdate
should be in the directory org\openmetadata\main
.
然后,当您运行 java
命令时,结构应该在类路径上 - 对于一个简单的例子,这只是意味着您当前的目录应该是 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
时,需要使用非斜杠句号指定完整的类名,例如
When running java
you need to specify the full classname using periods not slashes, i.e.
java org.openmetadata.main.OmadUpdate
这篇关于从命令行运行Java程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!