I have gone through this thread What causes and what are the differences between NoClassDefFoundError and ClassNotFoundException? This is what one of the ans,which has max u
NoClassDefFoundError is usually called when you are using a library(for example, Guava, Gson, CommonsIO). You put the library in classpath of your project, but you didn't export it together, you will get a NoClassDefFoundError when the application is running.
How to get NoClassDefFoundError:
Create a new project, with this class.
public class A
{
public void do()
{
System.out.println("Do!");
}
}
Export it as a .jarfile.
Now create another project. Add the exported jar file to classpath.
import ???.A;
public class Main
{
public static void main(String[] args)
{
A a = new A();
a.do();//NoClassDefFoundError thrown at here.
}
}
Export the project, make sure you do not include the jar file(with class A). Run the newly exported jar file, you will see that error!
The other answers in this thread are correct, i just want to add something i spent hours trying to figure out. Even if
Class.forName("apache.some.SomeLegitClass")
works,
Class.forName("apache.some.somelegitclass")
will result in a NoClassDefFoundError. The Class.forName() is case-sensitive. It will result in different exceptions if the classname is spelled wrong, or simply has incorrect casing.
As mentioned in previous answers, NoClassDefFoundError will occur when the class was present during the compile time and is not available during run time for some reasons.
There is another scenario I wish to add, which could also result in NoClassDefFoundError.
When you are trying to load a class which failed to load due to some exception say failure in static initialization block, system will throw you ExceptionInInitializerError. If you try to load the same class again (which failed to load previously), system will throw NoClassDefFoundError
Lets explore it with an sample
ClassWithStaticBlock.java
public class ClassWithStaticBlock {
static {
int total = 1/0;
}
}
Main.java
public class Main {
public static void main(String[] args) {
ClassWithStaticBlock cs;
try {
cs = new ClassWithStaticBlock();
}catch(Throwable e){
e.printStackTrace();
}
}
}
Result:
java.lang.ExceptionInInitializerError
at Main.main(Main.java:6)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Caused by: java.lang.ArithmeticException: / by zero
at ClassWithStaticBlock.<clinit>(ClassWithStaticBlock.java:7)
... 6 more
Lets modify Main.java
public class Main {
public static void main(String[] args) {
ClassWithStaticBlock cs;
try {
cs = new ClassWithStaticBlock();
}catch(Throwable e){
e.printStackTrace();
}
cs = new ClassWithStaticBlock(); //try to use ClassWithStaticBlock again
}
}
Result:
java.lang.ExceptionInInitializerError
at Main.main(Main.java:6)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Caused by: java.lang.ArithmeticException: / by zero
at ClassWithStaticBlock.<clinit>(ClassWithStaticBlock.java:7)
... 6 more
Exception in thread "Main Thread" java.lang.NoClassDefFoundError: ClassWithStaticBlock
at Main.main(Main.java:10)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
When we try to use ClassWithStaticBlock again (which failed to initialize earlier), System is throwing NoClassDefFoundError.
Found the sample from Why am I getting a NoClassDefFoundError in Java?