Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
467 views
in Technique[技术] by (71.8m points)

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

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). Also, I am building the app with Eclipse.

For some reason, I am getting a NoClassDefFoundError with the Derby jdbc class. I am assuming I would get a class not found exception was not found, so am guessing something else is happening. The derby class is part of the classpath, the WEB-INF/lib directory. What do you think the issue is?

My thoughts on the problem: It is not a "jar not found in the classpath" error but more an issue with Java or spring dynamically loading that class and when it gets loaded.

I am using Eclipse as the development tool but it probably isn't part of the problem. I am still using Maven on the command line and getting the same issue.

Here is the error:

WicketMessage: Can't instantiate page using constructor public wicketspring.easy.HomePage()

Root cause:

java.lang.NoClassDefFoundError: Could not initialize class org.apache.derby.jdbc.EmbeddedDriver
     at java.lang.Class.forName0(Native Method)
     at java.lang.Class.forName(Class.java:169)
     at org.apache.commons.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1130)
     at org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource.java:880)
     at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:113)
     at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:79)
     at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:577)
     at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:619)
     at wicketspring.easy.jdbc.JdbcWicketSpringHandler.data(JdbcWicketSpringHandler.java:39)
     at WICKET_wicketspring.easy.jdbc.JdbcWicketSpringHandler$$FastClassByCGLIB$$f1187cb6.invoke(<generated>)
     at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:191)
     at org.apache.wicket.proxy.LazyInitProxyFactory$CGLibInterceptor.intercept(LazyInitProxyFactory.java:319)
     at WICKET_wicketspring.easy.jdbc.JdbcWicketSpringHandler$$EnhancerByCGLIB$$e8f0e174.data(<generated>)
     at wicketspring.easy.HomePage.<init>(HomePage.java:91)
     at wicketspring.easy.HomePage.<init>(HomePage.java:47)

Here is the applicationContext.xml for Spring:

<beans>
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="org.apache.derby.jdbc.EmbeddedDriver" />
        <property name="url"><value>jdbc:derby:wicketspringdb</value></property>
        <property name="username"><value></value></property>
        <property name="password"><value></value></property>
    </bean> 
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource" />
    </bean> 

</beans>
<beans> 
    <import resource="classpath:common.xml"/>
    <bean id="jdbcHandler" class=wicketspring.easy.jdbc.JdbcWicketSpringHandler">
        <property name="jdbcTemplate" ref="jdbcTemplate" />
    </bean>

</beans>


...
Another stack trace system out.

Page.java:74) - At [2b] -- java.lang.NoClassDefFoundError: Could not initialize class org.apache.derby.jdbc.EmbeddedDriver
java.lang.NoClassDefFoundError: Could not initialize class org.apache.derby.jdbc.EmbeddedDriver
        at wicketspring.easy.HomePage.<init>(HomePage.java:72)
        at wicketspring.easy.HomePage.<init>(HomePage.java:47)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:
39)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorIm
pl.java:27)

...

Here is the java code, the code compiles and I can print out the class but I can't instantiate it. Strange?

Java Code:

package wicketspring.easy;

import java.util.List;

import org.apache.wicket.Application;
import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.proxy.IProxyTargetLocator;
import org.apache.wicket.proxy.LazyInitProxyFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import model.LoadableDetachableList;
import model.SelectionOptionBean;
import wicketspring.easy.jdbc.JdbcWicketSpringHandler;
import wicketspring.easy.service.IStateListService;
import wicketspring.easy.service.StateListServiceImpl;

import org.apache.derby.jdbc.EmbeddedDriver;

public class HomePage extends WebPage {

    private final JdbcWicketSpringHandler jdbcWicketSpringHandler;

    public HomePage(final PageParameters parameters) {

        super(parameters);        
        jdbcWicketSpringHandler = (JdbcWicketSpringHandler) LazyInitProxyFactory.createProxy(JdbcWicketSpringHandler.class,
                new IProxyTargetLocator() {
                    private static final long serialVersionUID = 1L;
                    public Object locateProxyTarget() {
                        return ((WicketApplication) Application.get()).context().getBean("jdbcHandler");
                    }
                });    

        /// WEIRD BECAUSE IT REACHES THIS POINT!!!
        LOGGER.debug("At [1] -- " + EmbeddedDriver.class);
        try {
            LOGGER.debug("At [2] -- " + new org.apache.derby.jdbc.EmbeddedDriver());
        } catch (NoClassDefFoundError ne) {
                // STACK TRACE ERROR HERE!!!!
            LOGGER.debug("At [2b] -- " + ne);
            ne.printStackTrace();
        } catch (Exception e) {
            LOGGER.debug("At [2] -- " + e);
            e.printStackTrace();
        }
        try {

            LOGGER.debug("At -- " + Class.forName("org.apache.derby.jdbc.EmbeddedDriver"));     
        } catch (NoClassDefFoundError ne) {
            LOGGER.debug("At [3b] -- " + ne);
            ne.printStackTrace();
        } catch (Exception e) {
            LOGGER.debug("At [3] -- " + e);
            e.printStackTrace();
        }

        /// ERROR FOR STACKOVERFLOW IS GENERATED FROM THIS LINE!!!!
        LOGGER.debug("At HomePage - jdbcHandler [3]: " + jdbcWicketSpringHandler.toStringJdbcTemplate());
        LOGGER.debug("At HomePage - jdbcHandler [3]: " + jdbcWicketSpringHandler.data());        

        ...

} // End of Class //

Edit: I might be missing a jar file that is a dependency of Spring Jdbc or dbcp.

Here is my WEB-INF/lib listing:

antlr-2.7.6.jar
aopalliance-1.0.jar
avalon-framework-4.1.3.jar
axis-1.4.jar
axis-jaxrpc-1.4.jar
cglib-nodep-2.2.jar
commons-collections-3.1.jar
commons-dbcp-1.2.2.jar
commons-logging-1.1.jar
commons-pool-1.3.jar
derby-10.6.1.0.jar
dom4j-1.6.1.jar
hibernate-core-3.5.1-Final.jar
jetty-6.1.4.jar
jetty-management-6.1.4.jar
jetty-util-6.1.4.jar
jta-1.1.jar
log4j-1.2.14.jar
logkit-1.0.1.jar
mx4j-3.0.1.jar
mx4j-tools-3.0.1.jar
servlet-api-2.5-6.1.4.jar
servlet-api-2.5.jar
slf4j-api-1.5.8.jar
slf4j-log4j12-1.4.2.jar
spring-2.5.6.jar
spring-aop-2.5.6.jar
spring-beans-2.5.6.jar
spring-context-2.5.6.jar
spring-core-2.5.6.jar
spring-jdbc-2.5.6.jar
spring-test-2.5.6.jar
spring-tx-2.5.6.jar
testRunWrapper-1.0.0.jar
wicket-1.4.13.jar
wicket-ioc-1.4.13.jar
wicket-spring-1.4.13.jar
xml-apis-1.0.b2.jar
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...