Compiling a Java program with javac succeeds, but NoClassDefFoundError on startup

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

Command line 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 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> 
+4
source share
3 answers

Nevermind, it works after use:

 java -classpath . HelloWorld 
+2
source

Your environment has the %CLASSPATH% environment variable. Get rid of it, it interferes with your java commands, this is the bad practice that Sun Oracle does anyway. As soon as you use the -classpath argument or its -cp shorthand, then %CLASSPATH% will be overridden. If the class path is not specified either by the environment variable or by arguments, then the current path will be taken by default (as you originally expected).

+3
source

run with the classpath specified in the current directory:

 java -cp . HelloWorld 
+1
source

All Articles