【发布时间】:2013-03-05 07:28:52
【问题描述】:
当我尝试从命令行运行我的程序时,我得到了 NoClassDefError。它在 Netbeans 中运行良好,并且 javac 可以正确编译。
我有一个名为 DistributedSystem 的类,到目前为止它只应该打印“hello.”。 目录为src/distributedsystem/,其中包含DistributedSystem.java,以及编译后的DistributedSystem.class。
如果我在 src/distributedsystem/ 中并运行
java DistributedSystem
然后我得到
Exception in thread "main" java.lang.NoClassDefFoundError: DistributedSystem (wrong name: distributedsystem/DistributedSystem)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
如果我在 src/distributedsystem/ 中运行,我也会得到完全相同的错误
java -cp . DistributedSystem
这是我认为可以解决问题的方法。我还尝试确保没有在其他地方设置类路径,即使 -cp 应该覆盖它。任何人都知道可能出了什么问题?
【问题讨论】:
-
能把 DistributedSystem.java 的代码贴出来吗?
-
假设类文件在
src/DistributedSystem中,你需要使用src文件夹中的java distributedsystem.DistributedSystem
标签: java linux command-line classpath noclassdeffounderror