ClassNotFoundException和NoClassDefFoundError

Stella981
• 阅读 614

定义:

对于术语的定义,最原汁原味的还是官方文档。

关于ClassNotFoundExceptionJavaDoc里这么描述:

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.

关于NoClassDefFoundErrorJavaDoc里这么描述:

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.


样例:

如果只看官方定义,不容易明白(我就是这样的),看例子:

public class A {
    public static void main(String[] args) {
        try {
            B b = new B(); //NoClassDefFoundError
        } catch(Throwable e) {
            e.printStackTrace();
        }
        System.out.println("----------------------------");
        try {
            Class b = Class.forName("B");//ClassNotFoundException
        } catch (Throwable e) {
            e.printStackTrace();
        }
        System.out.println("----------------------------");
        try {
            ClassLoader loader = A.class.getClassLoader();
            loader.loadClass("B");//ClassNotFoundException
        } catch(Throwable e) {
            e.printStackTrace();
        }
    }
}

class B {
    
}

这里定义了两个类A和B,其中在A中的main方法中使用了B。

如果使用Eclipse工具编写这个类,编写完按下保存后,Eclipse会自动编译这两个类生成A.class和B.class。

如果使用cmd命令行,则需要手动编译这两个类。

接下来,我们把编译好的B.class手动删掉,之后再执行A的main方法:

ClassNotFoundException和NoClassDefFoundError

可以看到,new B()这一句抛出了NoClassDefFoundError,而Class.forName("B")和ClassLoader.loadClass("B")这两句则抛出了ClassNotFoundException。

原因就是JavaDoc里提到的:当使用类名字符串去调用Class.forName等方法来加载一个类时,如果找不到这个类,则抛出ClassNotFoundException。如果JVM尝试去加载一个类(普通方法调用或者使用new关键字创建对象),而这个类又不存在时,则抛出NoClassDefFoundError。


区别:

一个是Error,一个是Exception。

通过类名,手动调用方法去加载不存在的类,抛出ClassNotFoundException;而如果是JVM调用方法加载不存在的类,抛出NoClassDefFoundError。


总结:

ClassNotFoundException :
Thrown when an application tries to load in a class through its name, but no definition for the class with the specified name could be found

NoClassDefFoundError :
Thrown if the Java Virtual Machine tries to load in the definition of a class and no definition of the class could be found.


参考:

http://stackoverflow.com/questions/1457863/what-causes-and-what-are-the-differences-between-noclassdeffounderror-and-classn

http://www.javaroots.com/2013/02/classnotfoundexception-vs.html

点赞
收藏
评论区
推荐文章
待兔 待兔
4个月前
手写Java HashMap源码
HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22
Wesley13 Wesley13
3年前
java8新特性整理
java9快来了,必须得梳理一下java8了。官方文档:http://docs.oracle.com/javase/specs/jls/se8/html/index.html(https://www.oschina.net/action/GoToLink?urlhttp%3A%2F%2Fdocs.oracle.com%2Fjavase%2Fspec
Stella981 Stella981
3年前
Linux系统小技巧(4):环境变量JAVA_TOOL_OPTIONS简介
_摘要:_ 对于通过JNI使用Java的应用,比如使用JNI来调用JVM。给JVM传递参数,环境变量JAVA\_TOOL\_OPTIONS很可能是你唯一的可选项。有关此环境变量的官方文档在此(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fdocs.oracle.com%2Fjava
Stella981 Stella981
3年前
Kafka的简单介绍_基础知识和应用场景介绍一下,一篇入门的文章
1.kafka介绍关于kafka入门的文章最好的就莫过于kafka的官方文档(https://www.oschina.net/action/GoToLink?urlhttp%3A%2F%2Fkafka.apache.org%2Fdocumentation.html)了,这上面对kafka的定义是:Kafkaisad
Stella981 Stella981
3年前
ClassNotFoundException和NoClassDefFoundError的区别
!(https://static.oschina.net/uploads/space/2018/0316/102545_BlhC_999023.png)1.NoClassDefFoundError是一个错误(Error),而ClassNOtFoundException是一个异常,在Java中错误和异常是有区别的,我们可以从异常中恢复程序但却不应该
Stella981 Stella981
3年前
NoClassDefFoundError && ClassNotFoundException
两种错误都是涉及类加载问题,类层次结构如下:!(https://oscimg.oschina.net/oscnet/c01027b28d3ecebe5cd027069ea4d949092.png)NoClassDefFoundError是系统错误,ClassNotFoundException是系统异常,可以捕获。NoClassDefFoundE
Stella981 Stella981
3年前
ExecutorService的十个使用技巧
ExecutorService的十个使用技巧ExecutorService(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fdocs.oracle.com%2Fjavase%2F8%2Fdocs%2Fapi%2Fjava%2Futil%2
Stella981 Stella981
3年前
REST Assured api
ClassRestAssured·        java.lang.Object(https://www.oschina.net/action/GoToLink?urlhttp%3A%2F%2Fdocs.oracle.com%2Fjavase%2F1.5.0%2Fdocs%2Fapi%2Fjava%2Flang%2FObject.ht
Wesley13 Wesley13
3年前
Java Monitoring&Troubleshooting Tools
JDKToolsandUtilities(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fdocs.oracle.com%2Fjavase%2F8%2Fdocs%2Ftechnotes%2Ftools%2Findex.html)MonitoringToolsYou
Stella981 Stella981
3年前
JVM 常用命令行参数
JVM的命令行参数参考:https://docs.oracle.com/javase/8/docs/technotes/tools/unix/java.html(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fdocs.oracle.com%2Fjavase%2F8%2Fdocs