Difference between ClassNotFoundException and NoClassDefFoundError

You have come across the above exception and error many times. You may also have read numerous articles about it but still many developers don’t get the clear idea. This article will explain you what is the difference between ClassNotFoundException and NoClassDefFoundError in simple way with example. Also, it is a one of the most frequently asked important basic interview questions. Even when you will become a developer in a company you might have encountered it several times.

ClassNotFoundException with Example

1) As shown in below code snippet I have one class “ClassNotFoundExceptionDemo” in which I am trying to load a class at run-time or dynamically.

2) First time I will try to load the dynamic class “java.lang.Thread” class which is always present in JDK/JRE so the Java program will find it. So there won’t be any exceptions. You can put any class as long as you configure the jar containing the class in project classpath. Best example is of database driver jars we use for JDBC connection.

3) Now we will try to load a class which is not present in any jar files in class path of the project.

So we will try to load a dummy class “java.lang.Thread1” which is not present in any jars or JDK/JRE configured in project. So see the output.

Conclusion

  1. When we try to load classes dynamically by using Class.forName(String className), JRE is not able to find them since the class file is not present in JDK/JRE or the jars are configured in classpath.
  2. Also one other thing is we have to compulsory catch the ClassNotFoundException because the JVM is not sure whether the class we are trying to load dynamically is present or not. Hence it forces us to catch the ClassNotFoundException in case the class is not present and we can handle the exception.

NoCalssDefFoundError with Example

1) To understand this properly I created 2 jars i.e. project1.jar and project2.jar and a class “NoClassDefFoundErrorDemo”. Now project1.jar contains a class file Project1.class and project2.jar contains class file Project2.class.

2) Below is the de-compiled class file which is present in a jar named project2.jar

Difference between ClassNotFoundException and NoClassDefFoundError 1

3) Below is the decompiled class file which is present in jar file named project1.jar

Difference between ClassNotFoundException and NoClassDefFoundError 2

While creating project1.jar I added project2.jar in classpath of Project1 as shown below because I used Project2 class in Project1 class.

Difference between ClassNotFoundException and NoClassDefFoundError 3

4) Below is the content of “NoClassDefFoundErrorDemo” java program.

5) In the project of “NoClassDefFoundErrorDemo” I added project1.jar and project2.jar in the classpath as shown below.

Difference between ClassNotFoundException and NoClassDefFoundError 4

6) Now when I run the code snippet in step 5 below output is shown.

7) Now I will remove the project2.jar from the classpath of project “NoClassDefFoundErrorDemo” as shown below.

Difference between ClassNotFoundException and NoClassDefFoundError 5

And now I will again run the code snipped present in step 5 and below output will be displayed.

8) As you can see we get the NoClassDefFoundError.

Conclusion

  1. While compiling and creating project1.jar I added project2.jar to classpath of Project1 so it got successfully compiled and was able to create project1.jar successfully.
  2. Now I added project1.jar to “NoClassDefFoundErrorDemo” and used Project1 class and I didn’t added project2.jar to classpath. So when running “NoClassDefFoundErrorDemo” will call Project1 from project1.jar and Project1 will use Project2 but since we didn’t added project2.jar to “NoClassDefFoundErrorDemo” we faced the error.
  3. Unlike ClassNotFoundException, NoClassDefFoundError is an error. You cannot catch this error even if you put the code snippet in try catch block.
  4. So in future if you face the same error it means may be a jar added in your project classpath is using another class of another jar which is not added on your project classpath.

Download this example hereThe link contains the git repository of the project.

Hope you like the article and feedback is highly appreciated.

You can also Subscribe here to stay updated on latest posts of OracleMine.com.

Leave a Reply

Your email address will not be published. Required fields are marked *