ClassNotFoundException和NoClassDefFoundError

ClassNotFoundException

ClassNotFoundException一個(gè)異常,該異常為已檢查異常(Checked Exception),可以在編譯期檢查到,需要用戶(hù)手工處理。

在Java API中這樣描述:

Thrown 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.

大致的意思是,當(dāng)應(yīng)用調(diào)用Class.forName()、ClassLoader中的findSystemClassloadClass方法時(shí),對(duì)應(yīng)名稱(chēng)的類(lèi)找不到時(shí),會(huì)拋出此異常。

例如:

public class ClassNotFoundExceptionDemo {
    public static void main(String[] args) throws ClassNotFoundException {
        Class<?> c = Class.forName("com.mysql.jdbc.Driver");
    }
}

/*
Exception in thread "main" java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
    at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:264)
    at liheng.exception.ClassNotFoundExceptionDemo.main(ClassNotFoundExceptionDemo.java:5)
 */

當(dāng)類(lèi)路徑中沒(méi)有名為com.mysql.jdbc.Driver的類(lèi),而我們由通過(guò)方法反射加載這個(gè)類(lèi)的時(shí)候,系統(tǒng)拋出ClassNotFoundException異常。

NoClassDefFoundError

NoClassDefFoundError是一個(gè)錯(cuò)誤,無(wú)法在編譯階段檢查到 。

在Java API中這樣描述:

Thrown if the Java Virtual Machine or a ClassLoader instance tries to load in the definition of a class (as part of a normal method call or as part of creating a new instance using the new expression) and no definition of the class could be found.

The searched-for class definition existed when the currently executing class was compiled, but the definition can no longer be found.

大致意思未,執(zhí)行過(guò)程中,JVM或ClassLoader嘗試加載一個(gè)類(lèi)的時(shí)候(調(diào)用類(lèi)的方法或者創(chuàng)建類(lèi)的對(duì)象),找不到某個(gè)在編譯器存在且成功編譯的類(lèi),此時(shí)會(huì)拋出NoClassDefFoundError錯(cuò)誤。

例如:

public class NoClassDefFoundErrorDemo {
    public static void main(String[] args) {
        A a = new A();
        a.print();
    }
}

class A {
    public void print() {
        System.out.println("A print");
    }
}

將.java文件編譯后會(huì)產(chǎn)生NoClassDefFoundErrorDemo.classA.class兩個(gè)類(lèi)文件,當(dāng)我們刪除A.class文件,并且運(yùn)行NoClassDefFoundErrorDemo類(lèi)時(shí):

/*
Exception in thread "main" java.lang.NoClassDefFoundError: liheng/exception/A
        at liheng.exception.NoClassDefFoundErrorDemo.main(NoClassDefFoundErrorDemo.java:5)
Caused by: java.lang.ClassNotFoundException: liheng.exception.A
        at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
        ... 1 more
*/

會(huì)拋出NoClassDefFoundError錯(cuò)誤,提示找不到liheng.exception.A類(lèi)的定義。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容