简体   繁体   English

java.lang.NoClassDefFoundError: 无法初始化类 | 静态块

[英]java.lang.NoClassDefFoundError: Could not initialize class | static block

I'm stuck with a strange issue where my JVM is throwing java.lang.NoClassDefFoundError.我遇到了一个奇怪的问题,我的 JVM 抛出了 java.lang.NoClassDefFoundError。 Any help in identifying the problem is greatly appreciated.非常感谢您在识别问题方面的任何帮助。

public class AccessorFactory
{
private static Map classMap = null;
    static
    {
        classMap = new HashMap(100);
        classMap.put("bills", Class.forName("com.example.BillAccessor"));
    }
}

public class BillAccessor
{
    class BillInnerClass
    {
        //some code
    }
}

I have 3 classes AccessorFactory.class, BillAccessor.class and BillAccessor$BillInnerClass.class.我有 3 个类 AccessorFactory.class、BillAccessor.class 和 BillAccessor$BillInnerClass.class。 All 3 are in same jar file and all are present in the classpath.所有 3 个都在同一个 jar 文件中,并且都存在于类路径中。

I've recently changed something in BillAccessor$BillInnerClass class and replaced this file in the jar file and restarted application instance in server (jboss).我最近更改了 BillAccessor$BillInnerClass 类中的一些内容,并在 jar 文件中替换了这个文件,并在服务器(jboss)中重新启动了应用程序实例。

Post restart I'm getting重启后我得到

java.lang.NoClassDefFoundError: Could not initialize class AccessorFactory

AccessorFactory class present in the jar file and in the class path. AccessorFactory 类存在于 jar 文件和类路径中。 Nothing else is changed except that I've replaced BillAccessor$BillInnerClass.除了我替换了 BillAccessor$BillInnerClass 之外,没有其他任何变化。

NoClassDefFoundError typically means the JVM has located the class (as opposed to ClassNotFoundException ), but unable to load it because of not having some dependent classes. NoClassDefFoundError通常意味着 JVM 已找到该类(与ClassNotFoundException相反),但由于没有一些依赖类而无法加载它。

Not sure if you tried rebuilding the entire jar (with all 3 classes) and use the new jar in JBoss?不确定您是否尝试重建整个 jar(包含所有 3 个类)并在 JBoss 中使用新 jar?

This is an old question, but I think it deserves a better answer.这是一个老问题,但我认为它值得一个更好的答案。

There are actually two things going on here.这里实际上发生了两件事。

  1. Some time before the exception that the Question is talking about, the JVM has attempted to execute the static initialization for the AccessorFactory class.在问题讨论的异常之前的一段时间,JVM 已尝试为AccessorFactory类执行静态初始化。 That has thrown an unchecked exception, causing initialization to fail.这引发了未经检查的异常,导致初始化失败。 As a result of that, the AccessorFactory class has been marked as failed.因此, AccessorFactory类被标记为失败。 It is effectively dead.它实际上已经死了。 The JVM will never retry the initialization. JVM 永远不会重试初始化。

  2. The exception you are seeing is an NoClassDefFoundError .您看到的异常是NoClassDefFoundError This has occurred because 1 the JVM has tried to load or initialize another class that depends on AccessorFactory .发生这种情况是因为1 JVM 试图加载或初始化另一个依赖于AccessorFactory的类。 The JVM has noted that AccessorFactory has been marked as failed, and thrown this exception. JVM 注意到AccessorFactory已被标记为失败,并抛出此异常。

So what caused the original problem?那么是什么导致了原来的问题呢?

Unfortunately, we can't say.不幸的是,我们不能说。 There is insufficient information 2 .资料不足2 The code in the static initializer is:静态初始化器中的代码是:

    classMap = new HashMap(100);
    classMap.put("bills", Class.forName("com.example.BillAccessor"));

It is highly unlikely that the first statement failed, but the second one is attempting to load a class.第一条语句失败的可能性很小,但第二条语句正在尝试加载一个类。 That could fail for a number of reasons, including the following.这可能由于多种原因而失败,包括以下原因。

  • It could fail to find the class at all.它可能根本找不到类。 That would result in a ClassNotFoundException .这将导致ClassNotFoundException

  • It could detect a fault in a .class file being loaded, that would result in an Error of some kind.它可以检测到正在加载的.class文件中的错误,这会导致某种Error

  • It could detect a binary version incompatibility between a .class file being loaded and some other class that has already been loaded.它可以检测到正在加载的.class文件与已加载的其他一些类之间的二进制版本不兼容。 This are a range of things that could cause this, but they will also result in an Error of some kind.这是一系列可能导致这种情况的事情,但它们也会导致某种Error


1 - The exception can happen for other reasons too, but in this case the exception's message tells us it is an earlier class initialization failure. 1 - 异常也可能由于其他原因而发生,但在这种情况下,异常的消息告诉我们这是较早的类初始化失败。
2 - The real reason was most likely reported in an earlier stacktrace which could be earlier in the OP's log file. 2 - 真正的原因很可能在较早的堆栈跟踪中报告,该堆栈跟踪可能在 OP 的日志文件中较早。


Having said that, I don't think that the code in the question is "real".话虽如此,我认为问题中的代码不是“真实的”。 Specifically, the static as shown should not compile .具体来说,如图所示的static不应该编译 The ClassNotFoundException thrown by forName is a checked exception and it must be caught. forName抛出的ClassNotFoundException是一个检查异常,它必须被捕获。 The JLS does not allow checked exceptions to propagate from a static block. JLS 不允许检查异常从static块传播。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.lang.NoClassDefFoundError:无法初始化类 - java.lang.NoClassDefFoundError: Could not initialize class java.lang.NoClassDefFoundError:无法初始化类 - java.lang.NoClassDefFoundError: Could not initialize class ECLIPSE:java.lang.NoClassDefFoundError:无法初始化类**** - ECLIPSE: java.lang.NoClassDefFoundError: Could not initialize class **** java.lang.NoClassDefFoundError:无法初始化类x - java.lang.NoClassDefFoundError: Could not initialize class x Tomcat java.lang.NoClassDefFoundError:无法初始化类 - Tomcat java.lang.NoClassDefFoundError: Could not initialize class java.lang.NoClassDefFoundError:无法初始化类DataLayer.HibernateAdapter - java.lang.NoClassDefFoundError: Could not initialize class DataLayer.HibernateAdapter java.lang.NoClassDefFoundError:无法初始化类business.HibernateUtil - java.lang.NoClassDefFoundError: Could not initialize class business.HibernateUtil Spring:java.lang.NoClassDefFoundError:无法初始化类 - Spring: java.lang.NoClassDefFoundError: Could not initialize class java.lang.NoClassDefFoundError:无法初始化类 XXX - java.lang.NoClassDefFoundError: Could not initialize class XXX java.lang.NoClassDefFoundError: 无法初始化 class - Kotlin Object - java.lang.NoClassDefFoundError: Could not initialize class - Kotlin Object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM