0
Sponsored Links


Ad by Google
This is again one of the popular java interview question, like which collections is better to store 10 millions of record between ArrayList and LinkedList, and almost every interviewer ask this question. Although, I have already listed Top 20 core java interview questions  and in this post going to list the key differences between ClassNotFoundException and NoClassDefFoundError both are sub class of java.lang.Throwable.
ClassNotFoundException - This exception is thrown when an application tries to load a class through its String name using any of the below methods, and no class definition is found with specified name.
  • forName("class name");
  • findSystemClass("class name");
  • loadClass("class name");

The ClassNotFoundException is a checked exception, below is the example to generate the ClassNotFoundException, you can read more about Checked Exception
public static void main(String[] args) throws ClassNotFoundException {

  Class.forName("abc");// abc does not exist.
 }
Run the above code, it will generate the below error,
Exception in thread "main" java.lang.ClassNotFoundException: abc
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:186)
at HelloWorld.main(HelloWorld.java:4)

NoClassDefFoundError - This exception is thrown, when java virtual machine or class loader tries to load the definition of a class and the definition of the class not found.
For example, at the compilation time source class file was present but, after compilation the .class file is somehow deleted, and class loader trying to load that .class file will caused of NoClassDefFoundError, below is the code to generate NoClassDefFoundError -
package com.javamakeuse.poc;

public class HelloWorld {
 public static void main(String... args) {
  System.out.println("Hello-World !");
  World world = new World();
  world.display();
 }
}

class World {
 public void display() {
  System.out.println("display");
 }
}
Now, compile the above program using javac HelloWorld.java, it will compile successfully, and generate two .class file HelloWorld.class and World.class.
Now remove the World.class file and try to execute the java HelloWorld command, it will gives you the below output.
Hello-World !
Exception in thread "main" java.lang.NoClassDefFoundError: World
        at HelloWorld.main(HelloWorld.java:5)
Caused by: java.lang.ClassNotFoundException: World
        at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
        ... 1 more

The NoClassDefFoundError is a sub-class of java.lang.Error, that means it's an unchecked exception, now let's see the key differences between the above topic-

Key Differences between ClassNotFoundException and NoClassDefFoundError

  1. The java.lang.ClassNotFoundException is a Checked Exception, whereas java.lang.NoClassDefFoundError is a sub-class of java.lang.Error that means NoClassDefFoundError  is an unchecked exception.
  2. The java.lang.ClassNotFoundException is thrown, when application tries to load classes, whereas java.lang.NoClassDefFoundError thrown when java virtual machine tries to load classes.
  3. The java.lang.ClassNotFoundException is recoverable, whereas java.lang.ClassDefFoundError is not recoverable.

That's about the differences, if you have any point to add, please share with me,I'll add into the list.
Sponsored Links

0 comments:

Post a Comment