`

java.lang.NoClassDefFoundError:could not initial class xxxxxxxxx

    博客分类:
  • java
阅读更多

当出现这个异常时,我的第一反应是,它跟ClassNotFoundException有什么不同。为了找到确切的答案我查看了它的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.

翻译过来就是,在调用方法或者new创建实例时,java虚拟机或者类加载器试图载入类定义,但是找不到类定义;编译当前执行类时,类是存在,但是现在找不到了;
分析

出了这个异常后,自然而然想到jar包中可能没有这个类,查看包中发现有这个类!如果没有这个类会抛出ClassNotFoundException;
继续深究,发现异常信息是java.lang.NoClassDefFoundError: could not initialize class:xxxxxx,自然而然想到java的类加载机制。
java类加载陆续经历,加载、验证、准备、解析、初始化等阶段,而上述异常表明是在初始化阶段出了问题。
在类的初始化阶段,执行方法(由编译器加入),它收集了静态代码块、静态变量赋值,收集顺序与源码中的顺序一致,执行完方法后,才完成类的加载。如果过程中出现错误,会导致类初始化失败。
基于这个原理,再回头看代码,发现类中的静态代码块中出现了异常,但是没有catch,导致出现异常。
解决方案

将static中的代码,try catch,并存入log,不妨碍类的加载,不然就会出现NoClassDefFoundError。
比如

static{

        try{
            //static initialize code
        }catch(Exception e){

        }
    }

 


疑问

在tomcat启动中,只报出NoClassDefFoundError,没有具体原因;但在javase中有根本原因,猜测是tomcat类加载器catch了异常细节,只给出了上层异常信息;

JavaSE做的测试

public class A {

    static {
        boolean flag = true;
        if (flag) {
            throw new RuntimeException("初始化类失败");
        }
    }

    public static void main(String[] args) {
    }
}

 


控制台打印

Exception in thread "main" java.lang.ExceptionInInitializerError
Caused by: java.lang.RuntimeException: 初始化类失败
    at testmaven.test.A.<clinit>(A.java:8)


结论

再回过头来看NoClassDefFoundError的API说明,就能看懂了;

    classnotfoundexception,是找不到类的二进制流,比如从jar中、网络中,找不到这个类;
    NoClassDefFoundError,类路径中有类,由于加载时静态代码块或者静态变量出现异常导致。对静态代码块进行try catch处理。
---------------------

原文:https://blog.csdn.net/wangjun5159/article/details/52716679

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics