views:

2710

answers:

2

I've put the Hibernate libraries in both the Glassfish domain and in the library collection of my project in Netbeans. hibernate-entitymanager.jar contains both HibernatePersistence (the last class in the call stack) and Ejb3Configuration, so I'm rather stumped as to why I get the missing class error for 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)
...
+5  A: 

I have never seen this specific error message before but I can explain a bit about what it means and give one possible cause.

The line

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);
            }
        }
    }
}

When I run class LoadingTest, I get the following output:

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 imported 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.
}

If the code in the finally block throws an exception, this exception will replace any exception thrown in the try block. I suggest this because a similar thing happened on this question. The stack trace posted in this question comes from within a finally block.

If my answer still doesn't help you, could you please post the entire stack trace you're seeing?

Pourquoi Litytestdata
Looks like it's really in the Log4J configuration, which isn't mentioned in the stack trace except when the server is first restarted.
Autocracy
+2  A: 

I had this issue as well, though it seems to be a version specific issue in my case. Newer versions of hibernate depend on Simple Logging Facade for Java (SLF4J), but the maven artifacts only include the API, so you need the runtime libraries in your WAR or the server's lib folder.

This is specific to the 3.4.0 version of Hibernate Entity Manager, though it may apply to other versions as well. If you're using Hibernate Core 3.3.x, you're on the 3.4.x series of Hibernate EM, so you NEED these runtime libraries.

Charles Chappell