ClassNotFoundException Vs NoClassDefFoundError in Java
ClassNotFoundException and NoClassDefFoundError both occur when class is not found at runtime. They are related to 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 application without updating 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 which is loaded by another classloader in Java. You must be wondering that what actually is classloader in Java. Java ClassLoader is a part of 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 system because of classloaders.
ClassNotFoundException is raised in below program as class “GeeksForGeeks” is not found in classpath.
// Java program to illustrate // ClassNotFoundException public class Example { public static void main(String args[]) { try { Class.forName("GeeksForGeeks"); } catch (ClassNotFoundException ex) { ex.printStackTrace(); } } } |
Output
java.lang.ClassNotFoundException: GeeksForGeeks
NoClassDefFoundError
NoClassDefFoundError occurs when class was present during compile time and program was compiled and linked successfully but class was not present during runtime. It is error which is derived from LinkageError. Linkage error occurs when a class has some dependencies on another class and latter class changes after compilation of 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.
Note: This program will not run on IDE. Try to run it on your own systems.
First make any two classes for a java program and link them.
// Java program to illustrate // NoClassDefFoundError class GeeksForGeeks { void greeting() { System.out.println("hello!"); } } class G4G { public static void main(String args[]) { GeeksForGeeks geeks = new geeksForGeeks(); geeks.greeting(); } } |
Above program will be successfully compiled and generate two classes GeeksForGeeks.class and G4G.class .
Now remove GeeksForGeeks.class file and run G4G.class.
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 is does not get updated with required JAR files while error occurs when required class definition is not present at runtime.
Recommended Posts:
- Java.util.LinkedList.poll(), pollFirst(), pollLast() with examples in Java
- Java.util.concurrent.RecursiveTask class in Java with Examples
- Java lang.Long.lowestOneBit() method in Java with Examples
- Java.util.function.IntPredicate interface in Java with Examples
- Java.util.LinkedList.offer(), offerFirst(), offerLast() in Java
- Java.util.concurrent.Phaser class in Java with Examples
- Java.util.concurrent.RecursiveAction class in Java with Examples
- Java lang.Long.reverse() method in Java with Examples
- Java.util.Collections.rotate() Method in Java with Examples
- Java.lang.Short toString() method in Java with Examples
- Java.util.function.LongPredicate interface in Java with Examples
- Java.util.BitSet class methods in Java with Examples | Set 2
- Java lang.Long.numberOfLeadingZeros() method in Java with Examples
- Java lang.Long.byteValue() method in Java with Examples
- Java.util.LinkedList.peek() , peekfirst(), peeklast() in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



