
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Differences Between ClassNotFoundException and NoClassDefFoundError in Java
In Java, both ClassNotFoundException and NoClassDefFoundError are issues that occur when the JVM or ClassLoader is not able to find the appropriate class at the time of loading (run-time). The ClassNotFoundException is a checked exception, and NoClassDefFoundError is an Error that comes under unchecked.
There are different types of ClassLoaders, each responsible for loading classes from different sources such as directories, JAR files, or network locations. If a required class is missing due to an incorrect classpath or a missing JAR file, the ClassLoader might fail to load it. This situation leads to one of these two issues.
The ClassNotFoundException in Java
The ClassNotFoundException is thrown when we try to load a class explicitly at runtime using Reflection, and that particular class is not found or missing in the classpath. There is nothing to check at compile-time since it is loading the class at run-time.
Example
Following is an example of ClassNotFoundException:
public class ClassNotFoundExceptionTest { public static void main(String[] args) { try { Class.forName("Test"); } catch (ClassNotFoundException cnfe) { System.err.println("You are trying to search for a class is not existing. "+cnfe); } } }
Output of the above Java program is as follows:
You are trying to search for a class is not existing. java.lang.ClassNotFoundException: Test
The NoClassDefFoundError
The NoClassDefFoundError occurs when a class has been compiled with a specific class from the classpath, but the same class is missing during run-time. The most basic reason to get this Error is when a class used by another class has been deleted or is no longer accessible during execution.
Example
The following Java program shows the NoClassDefFoundError:
class Test1 { public void show() { System.out.println("show() method called"); } } public class Test2 { public static void main(String[] args) { Test1 t = new Test1(); t.show(); } }
When we compile both the classes, it will generate two class files, Test1.class and Test2.class. While running the Test2 class, just remove the Test1.class file, then we will get NoClassDefFoundError as shown below:
Exception in thread "main" java.lang.NoClassDefFoundError: Test1 at Test2.main(Test2.java:9)
ClassNotFoundException VS NoClassDefFoundError
Now, let's discuss the difference between ClassNotFoundException and NoClassDefFoundError in Java:
ClassNotFoundException |
NoClassDefFoundError |
---|---|
It is a checked exception. Therefore, it must be handled in code using try-catch or declared using the throws keyword. |
It is an unchecked error that occurs due to issues in the runtime environment. |
Occurs when a dynamically requested class is missing. |
Occurs when a class was available at compile-time but is missing at runtime. |
Another reason could be an incorrect class name or a missing JAR at runtime. |
It can also occur due to missing class files or dependency issues at runtime. |
You can resolve it by adding the required class to the classpath. |
To resolve this, you need to make all required compiled classes available at run-time. |