Friday, July 2, 2021

Difference between NoClassDefFoundError vs ClassNotFoundExcepiton in Java

Both NoClassDefFoundError and ClassNotFoundException are dangerous errors that come when JVM or ClassLoader not able to locate class during the class loading process. Since different ClassLoader loads classes from a different location, sometimes this issue may be caused because of incorrect CLASSPATH as well i.e. some JAR files from lib are missing or from the old version. Though looks quite similar there is a subtle difference between NoClassDefFoundError and ClassNotFoundException, NoClassDefFoundError indicates that the class was present during the time of compilation but not available when you run Java program, sometimes error on static initializer block can also result in NoClassDefFoundError.

On the other hand, ClassNotFoundException is nothing to do with compile-time, ClassNotFoundException comes when you try to load a class in runtime using Reflection, like loading SQL drivers and corresponding Classloader is not able to find this class. 

Apart from this fundamental difference between NoClassDefFoundError and ClassNoFoundException in Java, let's see some more points for better understanding.




NoClassDefFoundError vs ClassNotFoundException

In short, both ClassNotFoundException and NoClassDefFoundError are caused by missing classes in CLASSPATH, usually due to missing JAR files, but if JVM initiates the process due to transitive reference and failed to load the class at runtime it throws java.lang.NoClassDefFoundError, while if your code explicitly tries to load the classes like by using Class.forName() method and class are not present in CLASSPATH, then JVM throws java.lang.ClassNotFoundException.


You will often find a NoClassDefFoundError actually caused by ClassNotFoundException, you can see it by looking for the "Caused By: " word in the error message.

Let's see some more differences between both of them in point form:

1) NoClassDefFoundError is an Error that is unchecked in nature, i.e. doesn't require try-catch or finally block. On the other hand, ClassNotFoundException is a checked Exception and requires mandatory handling using either try with catch block or try with the final block, failure to do so will result in a compile-time error.


2) If you are experiencing NoClassDefFoundError in the J2EE environment, there could be a host of reasons, one being multiple class loader and visibility of class among them. See 3 ways to solve NoClassDefFoundError for more details.


3) Often java.lang.ClassNotFoundException is thrown as result of following method call, Class.forName(), ClassLoader.findSystemClass() and ClassLoader.loadClass().


4) Another difference between NoClassDefFoundError and ClassNotFoundException is that NoClassDefFoundError is a LinkageError and can come during linking, while java.lang.ClassNotFoundException is an Exception and occurs during runtime.


Here is a nice slide of all the differences between java.lang.NoClassDefFoundError and java.lang.ClassNotFoundException in Java:

Difference between NoClassDefFoundError and ClassNotFoundException java



That's all on the difference between NoClassDefFoundError vs ClassNotFoundException in Java. Just remember this list of differences while debugging or troubleshooting NoClassDefFoundError or ClassNotFoundException, this will reduce confusion and help to solve the problem quickly.


Other Java Interview questions explanation from Java67

3 comments:

Feel free to comment, ask questions if you have any doubt.