Categories
java noclassdeffounderror

Why am I getting a NoClassDefFoundError in Java?

633

I am getting a NoClassDefFoundError when I run my Java application. What is typically the cause of this?

1

  • 1

    I believe it can also happen if you don’t run your java program with the correct syntax. For instance, you have to call your class from the root bin folder with the full package name (ie. my.package.myClass). I’d be more specific if I could but I’m not much of a java guy. I just remember messing this up a few times.

    Aug 29, 2008 at 15:06

294

This is caused when there is a class file that your code depends on and it is present at compile time but not found at runtime. Look for differences in your build time and runtime classpaths.

8

  • 1

    I had this error happen when putting a source file under the wrong namespace/package. I figured I could just put it anywhere, and the compiler was happy. Turns out I should have been more diligent for runtime to be happy as well.

    Oct 21, 2014 at 15:34

  • 2

    I had this error once when my server ran out of memory during a file upload. Every time I tried the upload, I’d get a different error. Eventually it told me I didn’t have enough heap space.

    Apr 25, 2016 at 7:47

  • 128

    This answer is not necessarily true and will be misleading to many people! See the better answer from Jared below.

    – Dave L.

    Aug 31, 2016 at 16:42

  • 5

    @DaveL. Thanks! Jared’s answer with 400+ upvotes is way below! One answer with -4 up(down?)votes is way above it. There is something fishy about SO’s answer ordering logic.

    Jul 31, 2017 at 14:00

  • 1

    This is a long-shot for someone, but I encountered this error because the class in question contained a SimpleDateFormat that was initialized with an invalid character (I had T in the middle instead of ‘T’).

    – Ryan D

    Jan 9, 2018 at 23:54

294

This is caused when there is a class file that your code depends on and it is present at compile time but not found at runtime. Look for differences in your build time and runtime classpaths.

8

  • 1

    I had this error happen when putting a source file under the wrong namespace/package. I figured I could just put it anywhere, and the compiler was happy. Turns out I should have been more diligent for runtime to be happy as well.

    Oct 21, 2014 at 15:34

  • 2

    I had this error once when my server ran out of memory during a file upload. Every time I tried the upload, I’d get a different error. Eventually it told me I didn’t have enough heap space.

    Apr 25, 2016 at 7:47

  • 128

    This answer is not necessarily true and will be misleading to many people! See the better answer from Jared below.

    – Dave L.

    Aug 31, 2016 at 16:42

  • 5

    @DaveL. Thanks! Jared’s answer with 400+ upvotes is way below! One answer with -4 up(down?)votes is way above it. There is something fishy about SO’s answer ordering logic.

    Jul 31, 2017 at 14:00

  • 1

    This is a long-shot for someone, but I encountered this error because the class in question contained a SimpleDateFormat that was initialized with an invalid character (I had T in the middle instead of ‘T’).

    – Ryan D

    Jan 9, 2018 at 23:54

143

Here is the code to illustrate java.lang.NoClassDefFoundError. Please see Jared’s answer for detailed explanation.

NoClassDefFoundErrorDemo.java

public class NoClassDefFoundErrorDemo {
    public static void main(String[] args) {
        try {
            // The following line would throw ExceptionInInitializerError
            SimpleCalculator calculator1 = new SimpleCalculator();
        } catch (Throwable t) {
            System.out.println(t);
        }
        // The following line would cause NoClassDefFoundError
        SimpleCalculator calculator2 = new SimpleCalculator();
    }

}

SimpleCalculator.java

public class SimpleCalculator {
    static int undefined = 1 / 0;
}

3

  • 4

    And the reason is that after first try jvm already knows its not going to work and throw different exception second time?

    – ikamen

    Jan 25, 2018 at 13:51


  • 1

    @ikamen Apparently it has stored somewhere the unsuccessful class initialisation of SimpleCalculator after the divide by zero? Does someone have a reference to the official documentation for this behaviour?

    Sep 27, 2018 at 20:40


  • 5

    @PhilipRego Not sure what you mean by a ‘pure’ NoClassDefFoundError. The first time new SimpleCalculator() is called, you get an ExceptionInInitializerError with a caused by of ArithmeticException. The second time you call new SimpleCalculator() you get a NoClassDefFoundError as pure as any other. The point is you can get a NoClassDefFoundError for a reason other than SimpleCalculator.class not being on the classpath at runtime.

    – harperska

    May 5, 2019 at 3:42