How to solve java.lang.NoClassDefFoundError?


I've tried both the example in Oracle's Java Tutorials. They both compile fine, but at run-time, both come up with this error:

Exception in thread "main" java.lang.NoClassDefFoundError: graphics/shapes/Square
    at Main.main(
Caused by: java.lang.ClassNotFoundException: graphics.shapes.Square
    at Method)
    at java.lang.ClassLoader.loadClass(
    at sun.misc.Launcher$AppClassLoader.loadClass(
    at java.lang.ClassLoader.loadClass(
    ... 1 more

I think I might have the file in the wrong folder. Here is the directory hierarchy:

├ shapes
|   ├
|   ├
├ linepoint
|   ├
|   ├
├ spaceobjects
|   ├
|   ├

And here is

import graphics.shapes.*;
import graphics.linepoint.*
import graphics.spaceobjects.*;

public class Main {
    public static void main(String args[]) {
        Square s = new Square(2,3,15);
        Line l = new Line(1,5,2,3);
        Cube c = new Cube(13,32,22);

What am I doing wrong here?


After I put put the Main class into the graphics package (I added package graphics; to it), set the classpath to "_test" (folder containing graphics), compiled it, and ran it using java graphics.Main (from the command line), it worked.

Really late UPDATE #2

I wasn't using Eclipse (just Notepad++ and the JDK), and the above update solved my problem. However, it seems that many of these answers are for Eclipse and IntelliJ, but they have similar concepts.

2/7/2017 9:32:42 AM

Accepted Answer

After you compile your code, you end up with .class files for each class in your program. These binary files are the bytecode that Java interprets to execute your program. The NoClassDefFoundError indicates that the classloader (in this case, which is responsible for dynamically loading classes, cannot find the .class file for the class that you're trying to use.

Your code wouldn't compile if the required classes weren't present (unless classes are loaded with reflection), so usually this exception means that your classpath doesn't include the required classes. Remember that the classloader (specifically will look for classes in package a.b.c in folder a/b/c/ in each entry in your classpath. NoClassDefFoundError can also indicate that you're missing a transitive dependency of a .jar file that you've compiled against and you're trying to use.

For example, if you had a class com.example.Foo, after compiling you would have a class file Foo.class. Say for example your working directory is .../project/. That class file must be placed in .../project/com/example, and you would set your classpath to .../project/.

Side note: I would recommend taking advantage of the amazing tooling that exists for Java and JVM languages. Modern IDE's like Eclipse and IDEA and build management tools like Maven or Gradle will help you not have to worry about classpaths (as much) and focus on the code! That said, this link explains how to set the classpath when you execute on the command line.

10/1/2017 6:55:03 AM

NoClassDefFoundError means that the class is present in the classpath at Compile time, but it doesn't exist in the classpath at Runtime.

If you're using Eclipse, make sure you have the shapes, linepoints and the spaceobjects as entries in the .classpath file.


if you got one of these error while compiling and running:

* NoClassDefFoundError

* Error: Could not find or load main class hello

* Exception in thread "main" java.lang.NoClassDefFoundError:javaTest/test/hello 
(wrong name: test/hello)
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(Unknown Source)
        at Source)
        at Source)
        at$100(Unknown Source)
        at$ Source)
        at$ Source)
        at Method)
        at 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)

-------------------------- SOLUTIION -----------------------

the problem is mostly in packages organization. You should arrange your classes in folders properly regarding to the package classifications in your source code.

On Compiling process use this command:

javac -d . []

To Run the class please use this command:

java [Package].[ClassName]


indicates, that something was found at compiletime but not at runtime. maybe you just have to add it to the classpath.


No Class Definition Exception occurs when the intended class is not found in the Class Path. At Compile Time Class : Class was generated from Java Compiler, But Somehow at Run Time the Dependent Class is not found.

Lets go through one Simple Example :

public class ClassA{
public static void main(String args[]){
     //Some gibberish Code...
     String text = ClassB.getString();
     System.out.println("Text is :" + text);


public class ClassB{
    public static String getString(){
      return "Testing Some Exception";

Now Lets assume that the above two Java Source Code are placed in some Folder let say "NoClassDefinationFoundExceptionDemo"

Now open a shell(Assuming Java is already being setup correctly)

  1. Go to Folder "NoClassDefinationFoundExceptionDemo"
  2. Compile Java Source Files javac ClassB javac ClassA
  3. Both Files are Compiled Sucessfully and generated Class files in the same Folder as ClassA.class and ClassB.class
  4. Now Since we are overiding ClassPath to current working directory therefore we execute the following command java -cp . ClassA and It worked Successfully and you will see the Output in the screen
  5. Now Lets say, You removed ClassB.class file from Present Directory. and now you execute the command again. java -cp . ClassA Now it will greet you with NoClassDefFoundException. as ClassB which is a dependency for ClassA is not found in the classpath(i.e present working directory).

NoClassDefFoundError in Java:


NoClassDefFoundError will come if a class was present during compile time but not available in java classpath during runtime. Normally you will see below line in log when you get NoClassDefFoundError: Exception in thread "main" java.lang.NoClassDefFoundError

Possible Causes:

  1. The class is not available in Java Classpath.

  2. You might be running your program using jar command and class was not defined in manifest file's ClassPath attribute.

  3. Any start-up script is overriding Classpath environment variable.

  4. Because NoClassDefFoundError is a subclass of java.lang.LinkageError it can also come if one of it dependency like native library may not available.

  5. Check for java.lang.ExceptionInInitializerError in your log file. NoClassDefFoundError due to the failure of static initialization is quite common.

  6. If you are working in J2EE environment than the visibility of Class among multiple Classloader can also cause java.lang.NoClassDefFoundError, see examples and scenario section for detailed discussion.

Possible Resolutions:

  1. Verify that all required Java classes are included in the application’s classpath. The most common mistake is not to include all the necessary classes, before starting to execute a Java application that has dependencies on some external libraries.

  2. The classpath of the application is correct, but the Classpath environment variable is overridden before the application’s execution.

  3. Verify that the aforementioned ExceptionInInitializerError does not appear in the stack trace of your application.


3 ways to solve java.lang.NoClassDefFoundError in Java J2EE

java.lang.NoClassDefFoundError – How to solve No Class Def Found Error