ClassNotFoundException和NoClassDefFoundError

定义:

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

关于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.


样例:

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

1public class A { 2 public static void main(String[] args) { 3 try { 4 B b = new B(); //NoClassDefFoundError 5 } catch(Throwable e) { 6 e.printStackTrace(); 7 } 8 System.out.println("----------------------------"); 9 try { 10 Class b = Class.forName("B");//ClassNotFoundException 11 } catch (Throwable e) { 12 e.printStackTrace(); 13 } 14 System.out.println("----------------------------"); 15 try { 16 ClassLoader loader = A.class.getClassLoader(); 17 loader.loadClass("B");//ClassNotFoundException 18 } catch(Throwable e) { 19 e.printStackTrace(); 20 } 21 } 22} 23 24class B { 25 26}

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

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

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

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

可以看到,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

点赞
收藏

评论区

加载中...

相关推荐

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

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

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

Kafka的简单介绍_基础知识和应用场景介绍一下,一篇入门的文章

1.kafka介绍关于kafka入门的文章最好的就莫过于kafka的官方文档(https://www.oschina.net/action/GoToLink?urlhttp%3A%2F%2Fkafka.apache.org%2Fdocumentation.html)了,这上面对kafka的定义是:Kafkaisad

ClassNotFoundException和NoClassDefFoundError的区别

!(https://static.oschina.net/uploads/space/2018/0316/102545_BlhC_999023.png)1.NoClassDefFoundError是一个错误(Error),而ClassNOtFoundException是一个异常,在Java中错误和异常是有区别的,我们可以从异常中恢复程序但却不应该

NoClassDefFoundError && ClassNotFoundException

两种错误都是涉及类加载问题,类层次结构如下:!(https://oscimg.oschina.net/oscnet/c01027b28d3ecebe5cd027069ea4d949092.png)NoClassDefFoundError是系统错误,ClassNotFoundException是系统异常,可以捕获。NoClassDefFoundE