本文介绍了在Web环境Spring / Wicket / Derby / Jetty中出现NoClassDefFoundError错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建一个简单的JDBC Spring Template应用程序,我正在使用的Web框架是wicket并且在jetty 6 web服务器下(通过Jetty Maven插件)。另外,我正在用Eclipse构建应用程序。

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.

出于某种原因,我得到了一个带有Derby jdbc类的NoClassDefFoundError。我假设我会找到一个未找到异常的类,所以我猜其他的事情正在发生。 derby类是类路径的一部分,即WEB-INF / lib目录。您认为这个问题是什么?

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?

我对问题的看法:这不是在类路径中找不到jar错误但是更多的是Java或Spring动态加载该类以及何时加载它的问题。

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.

我使用Eclipse作为开发工具但它可能不是问题的一部分。我仍然在命令行上使用Maven并遇到同样的问题。

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.

这是错误:

WicketMessage:无法使用构造函数实例化页面public wicketspring.easy.HomePage()

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

根本原因:

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)

这是Spring的applicationContext.xml:

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)

...

这是java代码,代码编译,我可以打印出类但我无法实例化它。奇怪?

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

Java代码:

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 //

编辑:
我可能错过了一个jar文件,它是Spring Jdbc或dbcp的依赖项。

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

这是我的WEB-INF / lib上市:

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


推荐答案

所以'无法初始化类org.apache.derby.jdbc.EmbeddedDriver'错误实际上是其他一些不太明显的类加载问题的主要症状。

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.

我正在使用Jetty作为Web服务器,Spring作为java6下的框架。

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

我认为有一个类加载问题,与MBeanServer类有关。

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

我确实忽略了启动时发生的错误:引起:java.lang.LinkageError:加载器约束违规:加载器(org / mortbay / jetty / webapp / WebAppClassLoader的实例)之前启动的加载用于java.lang.ClassLoader.defineClass1(Native M ethod)
at java.lang.ClassLoader.defineClassCond(ClassLoader.java:632)
at java.lang.ClassLoader.defineClass(ClassLoader.java:616)
at java.security.SecureClassLoader .defineClass(SecureClassLoader.java:141)

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)"

我在WEB-INF / lib目录中搜索了该类。它作为mx4j:jar的一部分包含在内。 Mx4j是jetty-management.jar的依赖项。我真的不需要jetty-management,所以我从我的pom文件中删除了该引用。

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.

基本上包含MBeanServer(来自mx4j)会导致某种类加载问题无法正确加载org.apache.derby.jdbc.EmbeddedDriver。我从我的Web应用程序中删除了它,应用程序开始正常工作。

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.

这篇关于在Web环境Spring / Wicket / Derby / Jetty中出现NoClassDefFoundError错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 11:15