Search code examples
javaclassloader

Very weird NoClassDefFoundError


I have a situation where I get a NoClassDefFoundError. Since there is not much information available, and I am 100% positive that the class is there, I made some detailed testing with Eclipse debugger:

  1. Start Java Program in Debug mode
  2. When it stops (somewhere), I check the classpath of the running thread (in debug mode). The directory of the "missing" class is there, and the .class file is also there.
  3. In the Expressions window, I add two expressions:

    a) new MyClass() --> leads to NoClassDefFoundError

    b) MyClass.class -> shows the Class Definition!?!?

  4. I put a breakpoint at the first line of the constructor of MyClass, and it never stops there, so the Constructor is never called... Calling private static final fields on the class works from the debug Window...

How can that be? I also spied the process with VisualVM and I can see the "MyClass" definition. What can cause such an error when the class is there?

I cannot get a small replicatable example of this case, since I have no clue what causes it. The unit tests of the class can instantiate it, this behaviour only happens in an integrated context.


Solution

  • This can occur if there is a failure while initializing the class, typically when we have an exception in static blocks so you should check all your static blocks to make sure that none of them throw a RuntimeException.