Issue with NoClassDefFoundError error in a web environment Spring/Wicket/Derby/Jetty

时光怂恿深爱的人放手 提交于 2019-11-29 16:31:28

The message Could not initialize class org.apache.derby.jdbc.EmbeddedDriver means that at that point the JVM has already tried and failed to initialize this class.

The JVM will fail to initialize a class if an exception is thrown and not caught within the class's static initializer. The only reasons that the JVM would be attempting to initializing the EmbeddedDriver class more than once would be:

  • some exception initializing the class was thrown, this exception was caught elsewhere and the program continued,
  • some exception initializing the class was thrown, but the program then entered a finally block, and within this finally block the JVM attempted to load the class again.

The static initializer for EmbeddedDriver (source) calls a boot() method. However, this boot() method calls a fair bit of other code, so it's difficult to tell where the problem could be. I had a look at some of the source of org.apache.commons.dbcp.BasicDataSource, but it seems the line numbers in your stacktrace don't agree with the source. I don't know which version of commons-dbcp you are using.

If you've got no other output messages nor stacktraces to go on, your best bet may be to attach the source of Derby to your debugger and step through it to see what's going on.


As an aside, it's quite possible to 'print' a class that hasn't been initialized. Consider the following classes:
class St1 {
    static {
        System.out.println("In static initializer");
    }
}

public class St2 {
    public static void main(String[] args) {
        System.out.println(St1.class);
        System.out.println(new St1());
    }
}

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

class St1
In static initializer
St1@65690726

So the 'Could not initialize class org.apache.derby.jdbc.EmbeddedDriver' error was actually the main symptom of some other, less obvious, class loading issues.

I was using Jetty as the web server and Spring as the framework under java6.

I believe there was a class loading issue, related to the MBeanServer class.

And I did ignore an error that happened at startup: "Caused by: java.lang.LinkageError: loader constraint violation: loader (instance of org/mortbay/jetty/webapp/WebAppClassLoader) previously initiated loading for a different type with name "javax/management/MBeanServer" at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClassCond(ClassLoader.java:632) at java.lang.ClassLoader.defineClass(ClassLoader.java:616) at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)"

I searched for the class in my WEB-INF/lib directory. It was included as part of mx4j:jar. Mx4j was a dependency for jetty-management.jar. I didn't really need jetty-management so I removed that reference from my pom file.

Basically the inclusion of MBeanServer (from mx4j) caused some kind of class loading issue where org.apache.derby.jdbc.EmbeddedDriver couldn't be loaded properly. I removed it from my web application and the application started working properly.

Sometimes these errors are because you have Derby in your classpath twice, somehow. With modern JDKs, Derby's drivers are 'auto-loaded', meaning that the JDK will look for JDBC drivers on the classpath and automatically load them. So you might check your system classpath as well as your application's libraries; perhaps you have a second copy of Derby hidden away somewhere in the path and the exception is trying to tell you that the two versions of Derby are in conflict.

Are you sure that your derby.jar contains the org.apache.derby.jdbc.EmbeddedDriver class? You may want to check you have the correct version.

If you are using Maven to package the application then it is strange that the jar name ends up being derby.jar with no version number attached to it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!