ClassNotFoundException Vs NoClassDefFoundError in Java | Code Factory


Index Page : Link

Donate : Link

Medium Blog : Link

Applications : Link

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

class Hello {
	public static void main(String[] args) {
        try {
            Class.forName("CodeFactory"); // will throw ClassNotFoundException
			//Class.forName("Hello"); // compile successfully
			//Class.forName("java.lang.Object"); // compile successfully
        }
        catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        }
	}
}

Output:

NoClassDefFoundError

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.

Example

// Java Program to Illustrate NoClassDefFoundError Exception
 
// Class 1
class CodeFactory {
    void greeting()
    {
        System.out.println("CodeFactory!");
    }
}
 
// Class 2
// Main class
class Hello {
    public static void main(String args[])
    {
        // Creating object of class 1 inside main() in class2
        CodeFactory obj = new CodeFactory();
        obj.greeting();
    }
}

Output:

 CodeFactory! 

Above program will be successfully compiled and generated two classes ‘CodeFactory.class’ and ‘Hello.class.’ Now remove CodeFactory.class file and run Hello.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 NoClassDefFoundError occurs when the required class definition is not present at runtime.

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s