Wednesday, May 3, 2017

ClassNotFoundException and NoClassDefFoundError in java

  1. java.lang.ClassNotFoundException This exception indicates that the class was not found on the classpath. This indicates that we were trying to load the class definition, and the class did not exist on the classpath.
  2. java.lang.NoClassDefFoundError Java Virtual Machine is not able to find a particular class at runtime which was available at compile time.
    If a class was present during compile time but not available in java classpath during runtime.
ClassNotFoundException is a checked Exception derived directly from java.lang.Exception class and you need to provide explicit handling for it while NoClassDefFoundError is an Error derived from LinkageError.

If you are using ClassLoader in Java and have two class loaders then if a ClassLoader tries to access a class which is loaded by another classloader will result in ClassNoFoundException.

ClassNotFoundException comes up when there is an explicit loading of class is involved by providing name of class at runtime using ClassLoader.loadClass(), Class.forName(),  while NoClassDefFoundError is a result of implicit loading of class because of a method call from that class or any variable access.

No comments:

Post a Comment

Creating mirror of BST