Fixing the “Exception in thread main java.lang.NoClassDefFoundError:” in Eclipse

When working in Eclipse, you may be working with the main method of a Java class. Every once in a while you will see the error:


Exception in thread "main" java.lang.NoClassDefFoundError: ...
Caused by: java.lang.ClassNotFoundException:

Usually you can fix this by running one of the following outside of Eclipse, from the project directory in a Windows Command Prompt. Or in Eclipse, you can try Project->Clean, Project->Build.


mvn clean
mvn compile
mvn test
mvn eclipse:clean
mvn eclipse:eclipse

The compile and test commands recreate the class files for “src/main/java” and “src/test/java”. The eclipse:clean and eclipse:eclipse commands recreate the .classpath and .project files that Eclipse uses.

This is a ClassPath issue. If you look in your project directory, you will see a “.classpath” file. If you open this, you will see all the places Java will look to try to find the .class file to run.

For this example, you might see:


  <classpathentry kind="src" path="src/test/java" output="target/test-classes" including="**/*.java"/>
  <classpathentry kind="src" path="src/test/resources" output="target/test-classes" excluding="**/*.java"/>
  <classpathentry kind="src" path="src/main/java" including="**/*.java"/>
  <classpathentry kind="src" path="src/main/resources" excluding="**/*.java"/>
  <classpathentry kind="output" path="target/classes"/>

Based on the first line above, what this means is that if you right click->Run As->Java Application from a Java file in the src/test/java directory, Java will look for the class in the specified output directory (ie target/test-classes). Java will take the package (ie com.mycompany.myapp) and class name (ie HelloWorld) and search for the class file accordingly (ie target/test-classes/com/mycompany/myapp/HelloWorld.class).

Based on the third and fifth lines above, if you right click->Run As->Java Application from a Java file in the src/main/java directory, Java will look for the class in the default output directory (ie target/classes). Java will take the package (ie com.mycompany.myapp) and class name (ie HelloWorld) and search for the class file accordingly (ie target/classes/com/mycompany/myapp/HelloWorld.class).

Running Java From the Command Prompt to Avoid Classpath Issues

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
  1. These commands can be run from any directory, meaning you don’t have to be in the directory where your HelloWorld.java file is.
  2. The first line compiles your HelloWorld.java file, creating a HelloWorld.class file.
  3. The second line runs the HelloWorld.class file.
  4. The -classpath tells java where to look for the specified file in each command.
  5. 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).
  6. 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:

  1. 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.
  2. You don’t have to enter the entire classpath (ie, you can just use -classpath “.”). Enter the directory you will be working in.
  3. 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 “.”) to run the file located at “C:\Users\Scott\workspace\myproject\HelloWorld.java“:


cd "C:\Users\Scott\workspace\myproject\"
javac -classpath "." HelloWorld.java
java -classpath "." HelloWorld

2 thoughts on “Fixing the “Exception in thread main java.lang.NoClassDefFoundError:” in Eclipse

    • The javac command needs the package attached to the class, as in com\mycompany\myapp\HelloWorld.java (see above).
      The java command also needs the package attached to the class, as in com.mycompany.myapp.HelloWorld (see above).

Leave a comment