【问题标题】:Java NoClassDefFoundError in Command Line命令行中的 Java NoClassDefFoundError
【发布时间】: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 应该覆盖它。任何人都知道可能出了什么问题?

【问题讨论】:

标签: java linux command-line classpath noclassdeffounderror


【解决方案1】:

转到文件夹 src 然后从那里编译然后运行

【讨论】:

  • 是的,这解决了问题。以为我已经尝试了所有组合,尴尬!谢谢。
【解决方案2】:

类路径应该指向基目录。看起来您正在尝试在包 distributedsystem 中运行类 DistributedSystem,但您的类路径设置为 project/bin/distributedsystem 而不是 project/bin

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-24
    • 1970-01-01
    • 2012-07-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多