Difference between ClassNotFoundException and NoClassDefFoundError 规格严格

Difference between ClassNotFoundException and NoClassDefFoundError
Priyanka_JTC |   Nov 18 2011 | Comments (0)  |  Visits (333)

Difference between ClassNotFoundException and NoClassDefFoundError

 

ClassNotFoundException is a checked Exception, Its thrown when that required class is not present in the location where classloader is looking.
When an application tries to load in a class through its string name using:
-The forName() method in class Class.
-The findSystemClass method() in class ClassLoader.
-The loadClass() method in class ClassLoader.
but no definition for the class with the specified name could be found.

This exception can be created using following program-


 
import java.net.URL; 

import java.net.URLClassLoader;

public class ClassNotFoundExceptionTest {

    public static void main(String args[]) {

        try {

            URLClassLoader loader = new URLClassLoader(new URL[] { new URL(

                "file://C:/CL/ClassNotFoundException/")});

            loader.loadClass("DoesNotExist");

        } catch (ClassNotFoundException e) {

            e.printStackTrace();

        } catch (MalformedURLException e) {

            e.printStackTrace();

        }

  }


 

 To avoid this error , User should check the following -
-     Make sure that
      Class is available in the logical class path of the class loader associated with the class.       
-     Make sure that
      Class Loader API is used properly .ie whether a wrong class Loader is engaged in Class.forname().
-     Make sure that dependent class of the class being loaded is visible to the class  loader.

 NoClassDefFoundError is an error .Its thrown to indicate that ClassLoader instance is trying to load in the definition of a class and no definition of the class is found. The searched-for class definition existed when the currently executing class was compiled, but the definition can no longer be found.

 This error can be recreated using following test program – 


public class NoClassDefFoundErrorTest {

    public static void main(String[] args) { 

        A a = new A(); 

    } 

}

public class A extends B { }

public class B { }

//Once you've compiled the above code remove the classfile for B. When //the code is executed, the following error occurs

//Exception in thread "main" java.lang.NoClassDefFoundError: B

 


Possible reasons of the error-
-      Bad format of the class
-      The version number of a class not matching

-       This can happen in the distribution or production of JAR files, where not all     

        the required class files were included.

 User should check whether a Class is not available on the classpath(not missing in the jar file). This problem can occur also when Class cannot load .
There are various reasons the class could not be loaded. Possible reasons include the following:
-     Failure to load the dependent class
-     The dependent class has a bad format.
-     The version number of the class is incorrect.

The difference between the two is that one is an Error and the other is an Exception. NoClassDefFoundError is an Error and it arises from fact the Java Virtual Machine having problems finding a class it expected to find. A program that was expected to work at compile-time can't run because of class files not being found, or is not the same as was produced or encountered at compile-time. This is a critical error, as the program cannot be initiated by the JVM.

On the other hand, the ClassNotFoundException is an Exception, which is  expected, and recoverable.

 

https://www.ibm.com/developerworks/mydeveloperworks/blogs/738b7897-cd38-4f24-9f05-48dd69116837/entry/difference_between_difference_between_classnotfoundexception_and_noclassdeffounderror29?lang=en

http://stackoverflow.com/questions/1457863/what-is-the-difference-between-noclassdeffounderror-and-classnotfoundexception
原文地址:https://www.cnblogs.com/diyunpeng/p/2520370.html