关于NoClassDefFoundError和ClassNotFoundException异常

时间:2022-07-22 19:13:10

java.lang.NoClassDefFoundError 和 java.lang.ClassNotFoundException 都是 Java 语言定义的标准异常。从异常类的名称看似乎都跟类的定义找不到有关,但是还是有些差异。我们先来看一下 java 规范中对这两个异常的说明:

java.lang.NoClassDefFoundError:

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.

出现这个异常,并不是说这个.class类文件不存在,而是类加载器试图加载类的定义时却找不到这个类的定义,实际上.class文件是存在的。

You are getting a java.lang.NoClassDefFoundError which does NOT mean that your class is missing (in that case you'd get a java.lang.ClassNotFoundException). The ClassLoader ran into an error while reading the class definition when trying to read the class.

java.lang.ClassNotFoundException:

Thrown when an application tries to load in a class through its string name using:
1. The forName method in class Class.
2. The findSystemClass method in class ClassLoader .
3. The loadClass method in class ClassLoader.
but no definition for the class with the specified name could be found.

从规范说明看, java.lang.ClassNotFoundException 异常抛出的根本原因是类文件找不到,缺少了 .class 文件,比如少引了某个 jar,解决方法通常需要检查一下 classpath 下能不能找到包含缺失 .class 文件的 jar。

但是,很多人在碰到 java.lang.NoClassDefFoundError 异常时也会下意识的去检查是不是缺少了 .class 文件,比如 SO 上的这位提问者(java.lang.NoClassDefFoundError: Could not initialize class XXX)-- “明明 classpath 下有那个 jar 为什么还报这个异常“。而实际上,这个异常的来源根本不是因为缺少 .class 文件。而碰到这个异常的解决办法,一般需要检查这个类定义中的初始化部分(如类属性定义、static 块等)的代码是否有抛异常的可能。

如果是 static 块,可以考虑在其中将异常捕获并打印堆栈等,或者直接在对类进行初始化调用(如 new Foobar())时作 try  catch。

static{
  try{ 
    ... your init code here
  }catch(Throwable t){
    LOG.error("Failure during static initialization", t);
  }
}