java.lang.NoClassDefFoundError in Java and possible reasons and solutions

NoClassDefFoundError can occur when the classloader cannot find the .class file that the program is trying to use. There are multiple possible reasons for this NoClassDefFoundError exception. Those reasons are:


  • ClassNotFoundException -- .class not found by the classloader
  • Class file available but exception occured while initializing static variables
  • Class file available but exception occured while initializing static blocks

Solution: 

The exception indicates that the classpath does not include required classes. The classloader (java.net.URLClassLoader) looks for classes in classpath. So setting classpath to the referenced classes jar would solve the exception. 

Comments