开发者

Compiling Java program with javac succeeds, but NoClassDefFoundError on run

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

Command prompt session:

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
Ca开发者_运维百科used 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>


Nevermind, it works after using:

java -classpath . HelloWorld


You've a %CLASSPATH% environment variable in your environment. Get rid of it, it's disturbing your java commands, it's a poor practice tought by Sun Oracle anyway. Once you use -classpath argument or its -cp shorthand, then the %CLASSPATH% will be overridden. If the classpath is not specified by either the environment variable or the arguments, then the current path will be taken as default (as you initially expected).


run with classpath specified to the current directory:

java -cp . HelloWorld
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜