0

所以基本上我正在使用 maven 以与我一直做的相同方式组织我的项目,但是我在 catalina.out 中收到一个错误,告诉我它无法在 com.mysql.jdbc 中找到 StringUtils 类,即使我可以看到jar 在战争类路径中。我在不同的服务器上没有遇到与 Tomcat7 相同的问题,这让我相信某处有一个库类掩盖了我在实际项目中拥有的 jar 文件。这是一个例外:

引起:java.lang.NoClassDefFoundError: com/mysql/jdbc/StringUtils

我的maven依赖如下:

<dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.20</version>
        </dependency>

当我可以在 WEB-INF 的 lib 部分中看到 jar 并且可以在 jar 中看到该类时,我只是无法理解如何在类路径中找不到该类。有人知道这里会发生什么吗?谢谢你的帮助。

如果您需要更多信息,请告诉我。

完整的堆栈跟踪:(我使用 Apache 的 BasicDataSource)。

    Throwable occurred: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'voiceManager' defined in class path resource [applicationContext.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.voiceforge.api.swift.VoiceManager]: Constructor threw exception; nested exception is org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot create JDBC driver of class 'com.mysql.jdbc.Driver' for connect URL 'jdbc:mysql://localhost/vf2?autoReconnect=true'
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:288)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1035)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:939)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:485)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:384)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:283)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:111)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3843)
    at org.apache.catalina.core.StandardContext.start(StandardContext.java:4338)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:791)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:771)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:525)
    at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:882)
    at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:522)
    at org.apache.catalina.startup.HostConfig.check(HostConfig.java:1283)
    at sun.reflect.GeneratedMethodAccessor66.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:37)
    at java.lang.reflect.Method.invoke(Method.java:611)
    at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:297)
    at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:848)
    at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:773)
    at org.apache.catalina.manager.ManagerServlet.check(ManagerServlet.java:1471)
    at org.apache.catalina.manager.HTMLManagerServlet.doPost(HTMLManagerServlet.java:250)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.filters.CsrfPreventionFilter.doFilter(CsrfPreventionFilter.java:186)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:530)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:852)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
    at java.lang.Thread.run(Thread.java:736)
Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.voiceforge.api.swift.VoiceManager]: Constructor threw exception; nested exception is org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot create JDBC driver of class 'com.mysql.jdbc.Driver' for connect URL 'jdbc:mysql://localhost/vf2?autoReconnect=true'
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:162)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:110)
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:280)
    ... 48 more
Caused by: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot create JDBC driver of class 'com.mysql.jdbc.Driver' for connect URL 'jdbc:mysql://localhost/vf2?autoReconnect=true'
    at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:80)
    at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:382)
    at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:456)
    at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:464)
    at com.voiceforge.api.dao.VoiceforgeDaoImpl.getVoiceList(VoiceforgeDaoImpl.java:86)
    at com.voiceforge.api.swift.VoiceManager.refreshVoices(VoiceManager.java:33)
    at com.voiceforge.api.swift.VoiceManager.<init>(VoiceManager.java:23)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:56)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:39)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:527)
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:147)
    ... 50 more
Caused by: org.apache.commons.dbcp.SQLNestedException: Cannot create JDBC driver of class 'com.mysql.jdbc.Driver' for connect URL 'jdbc:mysql://localhost/vf2?autoReconnect=true'
    at org.apache.commons.dbcp.BasicDataSource.createConnectionFactory(BasicDataSource.java:1452)
    at org.apache.commons.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1371)
    at org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1044)
    at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:111)
    at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:77)
    ... 61 more
Caused by: java.lang.NoClassDefFoundError: com/mysql/jdbc/StringUtils
    at com.mysql.jdbc.NonRegisteringDriver.parseURL(NonRegisteringDriver.java:582)
    at com.mysql.jdbc.NonRegisteringDriver.acceptsURL(NonRegisteringDriver.java:236)
    at java.sql.DriverManager.getDriver(DriverManager.java:443)
    at org.apache.commons.dbcp.BasicDataSource.createConnectionFactory(BasicDataSource.java:1437)
    ... 65 more
4

3 回答 3

3

NoClassDefFoundError可能是很多事情的征兆,但首先要检查的是该类实际上是在您部署的 web 应用程序的类路径上。

  • 检查 MySQL 连接器 JAR 是否在 webapp 的WEB-INF/lib目录中,并检查它是否实际包含com.mysql.jdbc.StringUtils异常表示它无法加载的类 ( )。

  • 检查 Web 容器的核心或共享库类路径中没有 JAR 文件的杂散副本。

  • 如果上述方法未能给出明确的原因,请检查日志以查找其他(早期)类加载异常。您向我们展示的那个可能存在嵌套异常。可能有更早的例外。例如,如果类初始化失败(由于未经检查的异常),那么所有后续加载该类或相关类的尝试都将抛出异常......而无需重新报告问题的原始原因。

另一种可能性是您的 WAR 文件 POM 正在合并到具有冲突依赖项的 JAR 中。或者您(实际上)允许使用依赖排除在模块构建链上进一步发生冲突。


stacktrace中有一条线索:

Caused by: java.lang.NoClassDefFoundError: com/mysql/jdbc/StringUtils
    at com.mysql.jdbc.NonRegisteringDriver.parseURL(NonRegisteringDriver.java:582)
    ....

请注意,NoClassDefFoundError不会在Class.forName(...)通话中引发。您应该获取您正在使用的 JAR 版本的源代码,并查看它在该行做什么。

我怀疑这是抛出异常,因为之前的类加载尝试失败了。日志文件中是否有任何早期的堆栈跟踪?

于 2012-06-09T01:08:18.353 回答
1

实际上,它可能不是找不到的 StringUtils 类。这可能会表现为 ClassNotFoundException。NoClassDefFoundError 有点不同,我的意思是找不到 StringUtils 依赖的东西。

于 2012-06-10T02:01:44.980 回答
0

我最终安装了 Tomcat 7,错误消失了。我不知道为什么这个错误会很不幸地出现,但希望这可以在未来帮助其他人。

于 2012-06-11T18:15:53.773 回答