I’ve looked through the other NoClassDefFoundError questions and not found a solution that works for me. I’m using the Eclipse IDE to write a program, and it runs fine when I right-click -> Run as Java application in Eclipse, but attempting to run it through the command line runs into the above error. The JRE I’m using is (I think) Java60. The program is compiled using javac HelloWorld.java which runs fine, then a call to java -cp . HelloWorld throws the error. Any help with troubleshooting this would be much appreciated.
Code is:
package tool;
public class HelloWorld {
public static void main(String[] args){
System.out.println("Hello World!");
}
}
Running from “C:\MADtool\HostDBtoMADTool\src\tool”
Output from the command line is:
C:\MADtool\HostDBtoMADtool\src\tool>java -cp . HelloWorld
Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld (wrong nam
e: tool/HelloWorld)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
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 sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
You enter the package “tools” and make a javac (which is correct), but then you try to start the class from within that package folder (what is wrong).
You have to change to the src-directory first, because the classpath is relative to the source-folder. If you use packages (what you really should do). You must ensure to start your code from the correct start-folder:
update
Take a look at classpath definitions: Wikipedia