使用javac编译Java程序成功,但运行时出现NoClassDefFoundError

时间:2010-11-20 16:18:04

标签: java javac

class HelloWorld
{ 
    public static void main(String[] args) 
    { 
        System.out.println("hey");
    } 
}

命令提示会话:

C:\Users\zobdos\Desktop>javac HelloWorld.java

C:\Users\zobdos\Desktop>dir *.class
 Volume in drive C is OS
 Volume Serial Number is A45E-7B01

 Directory of C:\Users\zobdos\Desktop

11/20/2010  10:16 AM               417 HelloWorld.class
               1 File(s)            417 bytes
               0 Dir(s)   8,145,432,576 bytes free

C:\Users\zobdos\Desktop>java HelloWorld
Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld
Caused by: java.lang.ClassNotFoundException: HelloWorld
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClassInternal(Unknown Source)
Could not find the main class: HelloWorld.  Program will exit.

C:\Users\btolbert\Desktop>

3 个答案:

答案 0 :(得分:3)

您的环境中有%CLASSPATH%个环境变量。摆脱它,它会扰乱你的java命令,无论如何,这是一个由 Sun Oracle做的糟糕做法。使用-classpath参数或其-cp简写后,%CLASSPATH%将被覆盖。如果环境变量或参数未指定类路径,则当前路径将被视为默认值(如您最初预期的那样)。

答案 1 :(得分:2)

没关系,它在使用后起作用:

java -classpath . HelloWorld

答案 2 :(得分:1)

运行时将classpath指定给当前目录:

java -cp . HelloWorld