Home » NoClassDefFoundError in Java

NoClassDefFoundError in Java

by Online Tutorials Library

NoClassDefFoundError in Java

Just like ClassNotFoundException, NoClassDefFoundError occurs at runtime. We get this error when the class is not available in the program at runtime. It is an unchecked exception which a program throws when the requested class is not present at runtime. In this case, the class was successfully compiled by the compiler, and when we try to run the program, the class will not be available. The ClassNotFoundException occurs when the classpath doesn’t get updated with the required JAR files. The NoClassDefFoundError is an error when the required class definition is not available at runtime.

The NoClassDefFoundError is available in LinkageError. What is LinkageError? If a class is dependent on another class and we made changes in that class after compiling the former class, we will get the LinkageError. Implicitly loading of class results NoClassDefFoundError. It results in this error because of accessing the variable or calling the method from that parent class. We cannot easily get the reason for this error occurring. It is also more difficult to debug this error too.

NoClassDefFoundError in Java

So, if we don’t want to get this error, we should always have to check all those classes which are dependent on this class.

Let’s take an example to understand the reason for getting this error.

NoClassDefFoundErrorExample.java

Output:

NoClassDefFoundError in Java

Explanation:

In the above code, we have a parent class. i.e., tutorAspire and a child class, i.e., SEO. The tutorAspire class has the show() method, and the SEO class has the showData() method. In the main method, we create an object of the SEO class and call both the methods, i.e., show and showData.

If we compile and run the above code, it will successfully compile and run and show us the desired output.

Now, we remove the SEO class code after compiling the program. So, our class will look like as after removing it:

Now, if we run the above code without compiling it, we will get the NoClassDefFoundError at runtime. So, this error is raised because right now, there is no SEO class.

NoClassDefFoundError in Java


You may also like