Monday, January 30, 2012

Command line Java NoClassDefFoundError


So I was having trouble with Java. Eclipse worked perfectly, but when I wanted to compile/run from the command line, I always got this:

Exception in thread "main" java.lang.NoClassDefFoundError: Adder/class
Caused by: java.lang.ClassNotFoundException: Adder.class
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
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)


So I looked up the error, and found this nice post:
http://www.codegravity.com/blog/noclassdeffounderror

The solution is to include the current path (.) in the classpath, such as:

java -classpath . Adder (Adder is the name of the class).
Tha's it.

No comments:

Post a Comment