博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
关于NoClassDefFoundError和ClassNotFoundException异常
阅读量:7082 次
发布时间:2019-06-28

本文共 1748 字,大约阅读时间需要 5 分钟。

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

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.

 

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 上的这位提问者()-- “明明 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);
  }
}

 

 

转载于:https://www.cnblogs.com/kofxxf/p/3706876.html

你可能感兴趣的文章
Nginx
查看>>
How To Enable‘root’Account Login Solaris 11 Directly
查看>>
利用 http协议代替ftp协议进行数据传输
查看>>
写一个监控平台的思路总结和实践
查看>>
LoadRunner socket脚本开发问题合集
查看>>
试玩win8体验新特性(翻译)
查看>>
一次带大字段表记录暴增(LOBSEGMENT)引发的悲剧
查看>>
MySql 源码安装
查看>>
rmlv出错处理一例
查看>>
MongoDB学习初步
查看>>
sccm 2007 r2 step by step 之十二 操作系统分发part1
查看>>
Tokyo Tyrant基本规范(1)--介绍和安装
查看>>
实验搭建 GitLab 平台
查看>>
oracle 11g dataguard环境搭建
查看>>
Puppet 实验六 多环境配置
查看>>
项目实战:LAMP环境+Xcache+Redis,另附Memcached配置。
查看>>
ORA-30036故障处理思路
查看>>
WINDOWS7更改访问windows共享的用户名和密码
查看>>
Advanced Threat Analytics 2016
查看>>
SFB 项目经验-31-批量为n台服务器导入PFX证书
查看>>