我已将 Hibernate 库放入 Glassfish 域和 Netbeans 项目的库集合中。 hibernate-entitymanager.jar 包含 HibernatePersistence(调用堆栈中的最后一个类)和 Ejb3Configuration,因此我很困惑为什么会出现 Ejb3Configuration 缺少类的错误。
java.lang.NoClassDefFoundError: Could not initialize class org.hibernate.ejb.Ejb3Configuration
at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:130)
at com.sun.enterprise.server.PersistenceUnitLoaderImpl.load(PersistenceUnitLoaderImpl.java:149)
at com.sun.enterprise.server.PersistenceUnitLoaderImpl.load(PersistenceUnitLoaderImpl.java:84)
...
最佳答案
我以前从未见过这个特定的错误消息,但我可以解释一下它的含义并给出一个可能的原因。
线路
java.lang.NoClassDefFoundError: Could not initialize class org.hibernate.ejb.Ejb3Configuration
doesn't mean that the JVM could not find the class org.hibernate.ejb.Ejb3Configuration
. It means that the JVM can find this class but it has already tried and failed to load this class.
It's the text Could not initialize class ...
that indicates that this has happened. If the JVM couldn't find the class at all you'd get something like the following instead:
java.lang.NoClassDefFoundError: org/hibernate/ejb/Ejb3Configuration
As an aside, it also means you're using Java 6 - in Java 5 the corresponding exception has no message.
The following two classes provide a demonstration of this behaviour. The class Unloadable
cannot be loaded because its static initializer always throws an exception. We attempt to load this class, catch the ExceptionInInitializerError
that results, and attempt to load Unloadable
again.
class Unloadable {
static {
if (true) { throw new RuntimeException(); }
}
}
public class LoadingTest {
public static void main(String[] args) throws Exception {
try {
Class.forName("Unloadable");
}
catch (ExceptionInInitializerError e) {
try {
Class.forName("Unloadable");
}
catch (NoClassDefFoundError e2) {
System.out.println("XXXXXXXXXXXXXXXXXXXXX");
e2.printStackTrace(System.out);
}
}
}
}
当我运行类LoadingTest
时,我得到以下输出:
XXXXXXXXXXXXXXXXXXXXX java.lang.NoClassDefFoundError: Could not initialize class Unloadable at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Class.java:169) at LoadingTest.main(LoadingTest.java:14)
I cannot say what is causing the original attempt to load org.hibernate.ejb.Ejb3Configuration
to fail. It could well be that Ejb3Configuration
itself depends on classes that are missing from the classpath. It might be worth going through the list of all the classes import
ed by Ejb3Configuration and ensuring that all those not under java.*
or javax.*
are within a JAR that Glassfish and Netbeans can see.
Also, I can only speculate as to why the JVM is attempting to load Ejb3Configuration
twice. When the class loading fails for the first time, an exception (typically some subclass of LinkageError
) is thrown. This type of exception isn't often caught, so my best guess is that something like the following is happening:
try {
// Some code that loads Ejb3Configuration and fails.
}
finally {
// Some code that also loads Ejb3Configuration and fails.
}
如果finally
block 中的代码抛出异常,则该异常将替换try
block 中抛出的任何异常。我建议这样做是因为 this question 上发生了类似的事情。此问题中发布的堆栈跟踪来自 finally
block 内。
如果我的回答仍然不能帮助您,您可以发布您看到的整个堆栈跟踪吗?
关于Glassfish 中的 hibernate - Ejb3Configuration NoClassDefFoundError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/899057/