exception in thread 'main' java.lang.NoClassDefFoundError:
Solution 1:
I found one another common reason. If you create the java file inside a package using IDE like eclipse, you will find the package name on the top of your java file like "package pkgName". If you try to run this file from command prompt, you will get the NoClassDefFoundError error. Remove the package name from the java file and use the commands in the command prompt. Wasted 3 hours for this. -- Abhi
Solution 2:
Exception in thread "main" java.lang.NoClassDefFoundError
One of the places java tries to find your .class
file is your current directory. So if your .class
file is in C:\java
, you should change your current directory to that.
To change your directory, type the following command at the prompt and press Enter:
cd c:\java
This .
tells java that your classpath is your local directory.
Executing your program using this command should correct the problem:
java -classpath . HelloWorld
Solution 3:
If your package is helloworld
you would go to parent dir of your package then run:
java helloworld.HelloWorld
Solution 4:
Run it like this:
java -jar HelloWorld.jar
Solution 5:
See http://scottizu.wordpress.com/2013/08/28/fixing-the-exception-in-thread-main-java-lang-noclassdeffounderror-in-eclipse/.
This is the long form of the Java commands that can be run from a Windows command prompt:
"C:\Program Files\Java\jdk1.6.0_18\bin\javac.exe" -classpath "C:\Users\Scott\workspace\myproject" com\mycompany\myapp\HelloWorld.java
"C:\Program Files\Java\jdk1.6.0_18\bin\java.exe" -classpath "C:\Users\Scott\workspace\myproject" com.mycompany.myapp.HelloWorld
- These commands can be run from any directory, meaning you don't have to be in the directory where your HelloWorld.java file is.
- The first line compiles your HelloWorld.java file, creating a HelloWorld.class file.
- The second line runs the HelloWorld.class file.
- The -classpath tells java where to look for the specified file in each command
- The Java compiler (javac.exe) expects the location of the java file, relative to the classpath (ie the file is located at C:\Users\Scott\workspace\myproject\com\mycompany\myapp\HelloWorld.java)
- Java (java.exe) expects the package (ie com.mycompany.myapp) and class (HelloWorld), relative to the classpath (ie the file is located at C:\Users\Scott\workspace\myproject\com\mycompany\myapp\HelloWorld.class)
Notice the classpath has no slash at the end. The javac.exe commands expects the file to end with ".java". The java.exe command expects the full class name and does not end with ".class".
There are a few ways to simplify these commands:
- You don't have to specify the entire path to java.exe. Add Java to the Windows Path (Run->sysdm.cpl->Advanced Tab->Environment Variables->Select Path->Edit->Append ";C:\Program Files\Java\jdk1.6.0_18\bin\"). Or you can append JAVA_HOME and create that Environment Variable.
-
You don't have to enter the entire classpath (ie, you can just use -classpath "."). Enter the directory you will be working in:
cd "C:\Users\Scott\workspace\myproject\"
You can use the default package (put the HelloWorld.java file directory in your working directory and don't use the Java package directive)
If you make these changes you would run something like this (and you might be able to leave out -classpath "."):
cd "C:\Users\Scott\workspace\myproject\"
javac -classpath "." HelloWorld.java
java -classpath "." HelloWorld