另一个 Java 类路径问题
我一直在 Netbeans 中从事一个项目。现在我想提交它并允许标记者使用脚本对其进行编译。但是,当我尝试通过命令行运行时,出现 NoClassDefFoundError 错误。即使手动将类路径设置为当前目录也是如此。
javac Main.java 工作正常,
然后调用 java -classpath 。主要给出:
java -classpath . Main
Exception in thread "main" java.lang.NoClassDefFoundError: Main (wrong name: pro
ject2/Main)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:620)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:12
4)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
at java.net.URLClassLoader.access$000(URLClassLoader.java:56)
at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:276)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
I've been working on a project in Netbeans. Now I'd like to submit it and allow the markers to compile it with a script. However, I get the NoClassDefFoundError when I try to run via the command line. Even when setting the classpath to the current directory manually.
javac Main.java works fine
then calling java -classpath . Main gives:
java -classpath . Main
Exception in thread "main" java.lang.NoClassDefFoundError: Main (wrong name: pro
ject2/Main)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:620)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:12
4)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
at java.net.URLClassLoader.access$000(URLClassLoader.java:56)
at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:276)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您似乎有一个名为
project2
的包。如果这是故意的,您应该在命令行上使用project2.Main
作为类名称,并且类路径包含project2
目录的父目录。如果不是故意的,请删除或替换源文件顶部的package project2;
。You appear to have a package named
project2
. If that is intentional, you should useproject2.Main
as the class name on the command line, and the classpath contain the parent of theproject2
directory. If it is not intentional, remove or replace thepackage project2;
at the top of the source file.这里的关键字是堆栈跟踪中的“错误名称”。你可以google一下,发现是包有问题。
The keyword here is the "wrong name" in the stack trace. You can google that and find out that there is a problem with packages.