ClassNotFoundException vs NoClassDefFoundError | How to fix ClassNotFoundException | How to fix NoClassDefFoundError

ClassNotFoundException and NoClassDefFoundError but these exceptions look very similar but are very different when comes to programming level. ClassNotFoundException and NoClassDefFoundError both occur at runtime when a particular class is not found by JVM, but the scenario in which they occur are different. In this tutorial, I will be discussing ClassNotFoundException and NoClassDefFoundError in details. Let’s get started.

First, let us under both these two terms one by one.

What is ClassNotFoundException

ClassNotFoundException occurs at runtime when the application loads the class using the Class.forName() or findSystemClass() or loadClass(), but the class is not found. These three methods return the class object which is associated with the class or interface. ClassNotFoundException is a checked exception.

What causes ClassNotFoundException

The common scenario when ClassNotFoundException occurs which most of the programmer will get is when we have not updated the classpath and the class with the specified name is not found in the classpath.

Suppose we want to make a database connection using oracle database and we are using Class.forName(“oracle.jdbc.driver.OracleDriver”) directly without adding the required jar files into the classpath. Then while loading the required oracle drivers the JVM will be unable to find the required driver class and will the ClassNotFoundException.

The example considering the same case is given below:

If the above program is run without making the required entries into the classpath, we will get the following exception in the console.

java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver

at java.net.URLClassLoader.findClass(Unknown Source)

at java.lang.ClassLoader.loadClass(Unknown source)

at sun.misc.Launcher$AppClassLoader.loadClass(Unknown source)

at java.lang.ClassLoader.loadClass(Unknown source)

at java.lang.Class.forName0(Native Method)

at java.lang.Class.forName(Unknown Source)

at pack1.MainClass.main(MainClass.java:4)


How to fix ClassNotFoundException

Whenever encountering this exception, we must very carefully examine the console logs

        1. As you can see in the example output, the very first line of the trace states the class which is missing.

        2. There can be cases when the missing class is not from your project, in such cases, we should determine if the class is from a third party jar. In such a case we should update the jar which includes the class.

What is NoClassDefFoundError

Like ClassNotFoundException, NoClassDefFoundError also occurs at runtime. But the scenario is different. Consider a case in which while building the application a class is present, but later on before running the application we removed it and we didn’t the application again and without re-building, we run the application at the time when the java runtime will search for that class it will be able to find the class and will throw NoClassDefFoundError.

What causes NoClassDefFoundError

NoClassDefFoundError comes when the class definition was available at compile time but absent at runtime. Consider the below example:

When we compile the following code two .class files will be generated one will be NoClassDefFoundErrorExample.class and another will be MainClass.class. Running the MainClass will not give the exception as of now but, if we delete the NoClassDefFoundErrorExample.class and then rerun the MainClass.class the JVM runtime will not be able to find the class and will throw NoClassDefFoundError. The exception will be as given below.

Exception in thread “main” java.lang.NoClassDefFoundError: NoClassDefFoundErrorExample

at MainClass.main(MainClass.java:7)

Caused by : java.lang.ClassNotFoundException: NoClassDefFoundErrorExample

at java.net.URLClassLoader.findClass(URLCLassLoader.java.381)

at java.lang.ClassLoader.loadClass(ClassLoader.java:424)

at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)

at java.lang.ClassLoader.loadClass(ClassLoader.java:357)

How to fix NoClassDefFoundError

When we get NoClassDefFoundError it means that the class loader can not find the .class file of that class. So, to rectify this error read the console carefully, in the console we will see “Cause by” line which will state the class name which the JVM runtime is not able to find. Add the .class file of the class or remove the class usage.

Conclusion

        1.     ClassNotFoundException is an exception found in java.lang.Exception package whereas NoClassDefFoundError is an error found in java.lang.error

        2.     ClassNotFoundException is thrown by methods like Class.forName(), findSystemClass(), loadClass() whereas NoClassDefFoundError is thrown by java runtime itself.

        3.     ClassNotFoundException occurs when the application tries to load class which is not in classpath whereas NoClassDefFoundError occurs when the class was present at compile time but later removed and is not present at runtime.

        4.     ClassNotFoundException occurs due to un-updated classpath file whereas NoClassDefFoundError occurs due to missing class file at runtime.

Further reading:

        1.      Core java interview questions.

        2.      Java 8 interview questions.

        3.      Internal working of Hashmap in java.

        4.      Abstraction in Java.

        5.      Encapsulation in java.

        6.      Compile time polymorphism in java.

        7.      Exception Handling in Java

Post a Comment

0 Comments