ClassNotFoundException Vs NoClassDefFoundError in Java Last Updated : 10 Nov, 2022 Comments Improve Suggest changes Like Article Like Report Both of the exceptions that are ClassNotFoundException and NoClassDefFoundError occur when the class is not found at runtime. They are related to the Java classpath. ClassNotFoundException ClassNotFoundException occurs when you try to load a class at runtime using Class.forName() or loadClass() methods and requested classes are not found in classpath. Most of the time this exception will occur when you try to run an application without updating the classpath with JAR files. This exception is a checked Exception derived from java.lang.Exception class and you need to provide explicit handling for it. This exception also occurs when you have two class loaders and if a ClassLoader tries to access a class that is loaded by another classloader in Java. You must be wondering that what actually is classloader in Java. Java ClassLoader is a part of the Java Runtime Environment that dynamically loads Java classes in JVM(Java Virtual Machine). The Java Runtime System does not need to know about files and files systems because of classloaders. Example Java // Java Program to Illustrate ClassNotFoundException public class GFG { // Main driver method public static void main(String args[]) { // Try block to check for exceptions try { Class.forName("GeeksForGeeks"); } // Catch block to handle exceptions catch (ClassNotFoundException ex) { // Displaying exceptions on console along with // line number using printStackTrace() method ex.printStackTrace(); } } } Output: ClassNotFoundException is raised in the above program as class "GeeksForGeeks" is not found in the classpath. NoClassDefFoundError Now dwelling on the next exception that is NoClassDefFoundError occurs when the class was present during compile time and the program was compiled and linked successfully but the class was not present during runtime. It is an error that is derived from LinkageError. Linkage error occurs when a class has some dependencies on another class and the latter class changes after compilation of the former class. NoClassFoundError is the result of implicit loading of class because of calling a method or accessing a variable from that class. This error is more difficult to debug and find the reason why this error occurred. So in this case you should always check the classes which are dependent on this class. In order to illustrate let us first make any two classes for a java program and link them. Example Java // Java Program to Illustrate NoClassDefFoundError Exception // Class 1 // Helper class class GeeksForGeeks { // Method void greeting() { // Print statement whenever method is called System.out.println("hello!"); } } // Class 2 // Main class class GFG { // Main driver method public static void main(String args[]) { // Creating object of class 1 // inside main() in class2 GeeksForGeeks geeks = new GeeksForGeeks(); // Calling method of above class geeks.greeting(); } } Outputhello! Output explanation: Above program will be successfully compiled and generated two classes 'GeeksForGeeks.class' and 'GFG.class.' Now remove GeeksForGeeks.class file and run GFG.class when we saw that at Java runtime NoClassDefFoundError will be thrown. ClassNotFoundException Vs NoClassDefFoundError As the name suggests, ClassNotFoundException is an exception while NoClassDefFoundError is an error.ClassNotFoundException occurs when classpath does not get updated with required JAR files while error occurs when the required class definition is not present at runtime. Comment More infoAdvertise with us Next Article ClassNotFoundException Vs NoClassDefFoundError in Java N NishuAggarwal Follow Improve Article Tags : Java Difference Between Java-Exceptions Practice Tags : Java Similar Reads How to Solve java.lang.ClassNotFoundException in Java? In Java, java.lang.ClassNotFoundException is a checked exception and occurs when the Java Virtual Machine (JVM) tries to load a particular class and the specified class cannot be found in the classpath. ClassNotFoundException should be handled with a try-catch block or using the throw keyword. In ol 4 min read User-Defined Custom Exception in Java In Java, an Exception is an issue (run-time error) that occurs during the execution of a program. When an exception occurs, the program terminates abruptly, and the code beyond the exception never gets executed.Java provides us the facility to create our own exceptions by extending the Exception cla 3 min read Method Class | getExceptionTypes() Method in Java The java.lang.reflect.Method.getExceptionTypes() method of "Method class" returns an array of Exception Type Class Objects declared to be thrown by the method object to handle exception inside the method. All the exceptions handled by method using thrown clause, are returned as array of Class object 3 min read Method Class | getGenericExceptionTypes() Method in Java The getGenericExceptionTypes() method of java.lang.reflectMethod class returns an array of Type objects representing Exceptions thrown by the method object to handle exception. All the exceptions handled by method using thrown clause are returned as array of Type objects using this method. This meth 4 min read Null Pointer Exception in Java A NullPointerException in Java is a RuntimeException. It occurs when a program attempts to use an object reference that has the null value. In Java, "null" is a special value that can be assigned to object references to indicate the absence of a value.Reasons for Null Pointer ExceptionA NullPointerE 5 min read Like