I am trying to build a simple JDBC Spring Template application, the web framework I am using is wicket and under the jetty 6 web server (through the Jetty Maven Plugin). Al
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.
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.
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:
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.
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