当我在 Linux 中从命令行运行 Java 类文件时,为什么会收到 NoClassDefFoundError 错误?
我正在尝试从 Linux 命令行运行 test.class
文件。我使用 javac test.java 编译了该文件,生成了 test.class 文件。当我运行命令 java test
时,它会抛出一个类未找到异常。我也尝试指定包名称,结果相同。这是输出,有人可以帮忙吗?根据谷歌搜索,我相信我的语法是正确的。
[root@localhost usr]# java test
Exception in thread "main" java.lang.NoClassDefFoundError: test (wrong name: testJava/test)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:634)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:277)
at java.net.URLClassLoader.access$000(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:212)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
Could not find the main class: test. Program will exit.
I am trying to run a test.class
file from the linux command line. I compiled the file using javac test.java
which produced the test.class
file. When I run the command java test
it throws a class not found exception. I also tried specifying the package name with the same result. Here is the output, can anybody help? I believe my syntax is correct according to Google searches.
[root@localhost usr]# java test
Exception in thread "main" java.lang.NoClassDefFoundError: test (wrong name: testJava/test)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:634)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:277)
at java.net.URLClassLoader.access$000(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:212)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
Could not find the main class: test. Program will exit.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您需要使用 javac 编译您的 .java 文件:
然后使用 java 运行该文件
如果您执行此操作但仍然不起作用,则意味着您需要确保该文件和文件内的类名具有相同的名称名称...所以 MyFile.java 包含
You need to compile your .java file with javac:
Then run the file using java
If you're doing this and it still doesn't work, that means you need to make sure the file and the class name inside the file have the same name... so MyFile.java contains
使用
testJava 父目录中的
java testJava.test
Use
java testJava.test
from the parent directory of testJava假设您的类如下所示:
然后要编译并运行该文件,您基本上会这样做:
这意味着您必须使用其完全限定名称运行该类,其中包括该类的包名称。您还必须从包含包根目录的目录执行此操作。 (除非您使用
-cp
标志指定“根”目录。)Assuming your class looks like this:
Then to compile and run the file, you basically do this:
What this means is that you have to run the class with its fully qualified name, which includes the package name of the class.You also have to do it from the directory that conatins the root of your package. (Unless you specify the "root" directory with the
-cp
flag.)最初我也遇到了问题。
java -cp 。测试
I too had problem initially.
java -cp . Test