by BehindJava
What is java.lang.NoClassDefFoundError in Java and How to resolve it
In this blog, we are going to learn about NoClassDefFoundError and its resolution.
NoClassDefFoundError
- This error occurs when the class is present at the compile time in which the class is compiled and linked successfully. But the class was not present at the runtime.
- It is derived from linkage error.
Example: Lets say, we have two classes TestA and TestB where we create TestA object in the TestB as shown below.
class TestA {
public void aMethod() {
System.out.println("Inside method a of TestA class");
}
}
public class TestB {
public static void main(String[] args) {
TestA a = new TestA();
a.aMethod();
}
}
When you compile the above program it runs and gives the output as below i.e., After the compilation you can see the class files generated. Output:
Inside method a of TestA class
Now delete the TestA.class file and run the TestB and you can see the following output.
Exception in thread "main" java.lang.NoClassDefFoundError: ds/TestA
at ds.TestB.main(TestB.java:13)
Caused by: java.lang.ClassNotFoundException: ds.TestA
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:636)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:182)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:519)
... 1 more