tomcat7:无法加载 JDBC 驱动程序类 [com.mysql.jdbc.Driver]

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12191900/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 14:42:26  来源:igfitidea点击:

tomcat7: Could not load JDBC driver class [com.mysql.jdbc.Driver]

mysqlspringtomcattomcat7

提问by Javier Sedano

I've seen several similar questions on stackoverflow, but they didn't solved my problem. This oneis specially useful, since it points to official tomcat documentationand specially this section.

我在 stackoverflow 上看到了几个类似的问题,但它们并没有解决我的问题。这个特别有用,因为它指向官方的tomcat 文档,特别是本节

The solutions provided there is, in short: tomcat needs jdbc database drivers to be copied to $CATALINA_HOME/lib, because it will not find them under WEB-INF/lib/. Ok, but it still does not work for me, and I am becoming mad.

提供的解决方案有,总之:tomcat的需要被复制到JDBC数据库驱动程序$CATALINA_HOME/lib,因为它不会下找到它们WEB-INF/lib/。好吧,但它仍然对我不起作用,而且我快疯了。

Let's see if you can provide any further idea.

让我们看看您是否可以提供任何进一步的想法。

The environment is Windows XP, Tomcat7, Eclipse Indigo, Java6 and Spring3.

环境为Windows XP、Tomcat7、Eclipse Indigo、Java6 和 Spring3

mysql-connectorhas been copied to $CATALINA_HOME/lib

mysql-connector已复制到 $CATALINA_HOME/lib

C:\>dir "c:\Program Files\Apache-Tomcat-7.0.12\lib"\mysql*
[...]
/2012  13:39           877.094 mysql-connector-java-5.1.21.jar
[...]
C:\>

You probably know that Eclipse's Tomcat integration creates a WTP-specific directory for its deployments (CATALINA_BASE), so I copied mysql-connector also there (I've unsuccessfully tried with the mysql-conector.jarin CATALINA_HOME, in CATALINA_BASEand in both of them):

您可能知道 Eclipse 的 Tomcat 集成为其部署创建了一个特定于 WTP 的目录 ( CATALINA_BASE),所以我也在那里复制了 mysql-connector(我尝试使用mysql-conector.jarin CATALINA_HOME、inCATALINA_BASE和 in 都没有成功):

C:\>dir c:\Share\genesis\wsEclipse-indigo\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\lib
[...]
29/08/2012  13:39           877.094 mysql-connector-java-5.1.21.jar
[...]
C:\>

As far as I understand Tomcat's catalina.properties file, they are both added to the classpath, so I did not expect any difference, but I tried, just in case.

据我了解 Tomcat 的 catalina.properties 文件,它们都被添加到类路径中,所以我没想到有什么区别,但我尝试了,以防万一。

My spring datasource definition is very simple:

我的 spring 数据源定义非常简单:

  <bean id="securityDataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost:3306/venus" />
    <property name="username" value="root" />
    <property name="password" value="" />
  </bean>

But when I restart tomcat I get the following error:

但是当我重新启动tomcat时,出现以下错误:

ERROR: org.springframework.web.context.ContextLoader - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'securityDataSource' defined in ServletContext resource [/WEB-INF/spring/root-context.xml]: Error setting property values; nested exception is org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (1) are:
PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property 'driverClassName' threw exception; nested exception is java.lang.IllegalStateException: Could not load JDBC driver class [com.mysql.jdbc.Driver]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1396)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1118)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    at org.springframework.beans.factory.support.AbstractBeanFactory.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:4701)
    at org.apache.catalina.core.StandardContext.call(StandardContext.java:5204)
    at org.apache.catalina.core.StandardContext.call(StandardContext.java:5199)
    at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (1) are:
PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property 'driverClassName' threw exception; nested exception is java.lang.IllegalStateException: Could not load JDBC driver class [com.mysql.jdbc.Driver]
    at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:102)
    at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:58)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1393)
    ... 21 more
30-ago-2012 8:38:11 org.apache.catalina.core.StandardContext listenerStart
GRAVE: Excepción enviando evento inicializado de contexto a instancia de escuchador de clase org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'securityDataSource' defined in ServletContext resource [/WEB-INF/spring/root-context.xml]: Error setting property values; nested exception is org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (1) are:
PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property 'driverClassName' threw exception; nested exception is java.lang.IllegalStateException: Could not load JDBC driver class [com.mysql.jdbc.Driver]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1396)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1118)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    at org.springframework.beans.factory.support.AbstractBeanFactory.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:4701)
    at org.apache.catalina.core.StandardContext.call(StandardContext.java:5204)
    at org.apache.catalina.core.StandardContext.call(StandardContext.java:5199)
    at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (1) are:
PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property 'driverClassName' threw exception; nested exception is java.lang.IllegalStateException: Could not load JDBC driver class [com.mysql.jdbc.Driver]
    at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:102)
    at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:58)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1393)
    ... 21 more

One more hint: using Sysinternals Process Explorer I can see that the mysql-connector-java.jaris being open by Tomcat:

还有一个提示:使用 Sysinternals Process Explorer 我可以看到mysql-connector-java.jarTomcat 正在打开:

"C:\Program Files\Java\jre6\bin\javaw.exe" -Dcatalina.base=C:\Share\genesis\wsEclipse-indigo\.metadata\.plugins\org.eclipse.wst.server.core\tmp0 "-Dcatalina.home=C:\Program Files\Apache-Tomcat-7.0.12" -Dwtp.deploy=C:\Share\genesis\wsEclipse-indigo\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps "-Djava.endorsed.dirs=C:\Program Files\Apache-Tomcat-7.0.12\endorsed" -Dfile.encoding=Cp1252 -classpath "C:\Program Files\Apache-Tomcat-7.0.12\bin\bootstrap.jar;C:\Program Files\Apache-Tomcat-7.0.12\bin\tomcat-juli.jar" org.apache.catalina.startup.Bootstrap start

Tomcat @ Process Explorer

Tomcat@进程资源管理器

Any further idea?

还有什么想法吗?



EDIT1:I've tried to use Oracle and it works!

EDIT1:我尝试使用 Oracle 并且它有效!

C:\>dir "c:\Program Files\Apache-Tomcat-7.0.12\lib"\oci*
[...]
02/10/2006  22:36         1.545.954 ocijdbc10.jar
[...]
C:\>

The datasource:

数据源:

  <bean id="securityDataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
    <property name="url" value="jdbc:oracle:oci:@BARVDB002D:1545:TFSDB0" />
    <property name="username" value="me" />
    <property name="password" value="qwerty" />
  </bean>

What am I doing wrong for mysql?

我对mysql做错了什么?

采纳答案by Javier Sedano

Solved!

解决了!

Arturs Licis' suggestion was the key. Somehow the mysql driver had been incorrectly downloaded. It seemed a .jar file, but failed to load. Tomcat was showing just the first level of the exception:

Arturs Licis 的建议是关键。不知何故,mysql 驱动程序下载不正确。它似乎是一个 .jar 文件,但无法加载。Tomcat 仅显示第一级异常:

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
[...]

But when I coded a very simple helloworld with Class.forName("com.mysql.jdbc.Driver"), the following error was shown instead:

但是当我用 Class.forName("com.mysql.jdbc.Driver") 编写一个非常简单的 helloworld 时,反而显示了以下错误:

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
[...]
Caused by: java.util.zip.ZipException: invalid LOC header (bad signature)
[...]

Eureka! Opening the .jar file on WinRar shows that it can be open... but with some errors.

尤里卡!在 WinRar 上打开 .jar 文件显示它可以打开......但有一些错误。

Downloading the driver again did solve the problem.

再次下载驱动确实解决了问题。

Downvote for myself :-(

为自己投反对票:-(

回答by Ralph

The solutions provided there is, in short: tomcat needs jdbc database drivers to be copied to $CATALINA_HOME/lib/, because it will not find them under WEB-INF/lib/. Ok, but it still does not work for me, and I am becoming mad.

提供的解决方案有,总之:tomcat的需要被复制到JDBC数据库驱动程序$CATALINA_HOME/lib/,因为它不会下找到它们WEB-INF/lib/。好吧,但它仍然对我不起作用,而且我快疯了。

This depends on how you load the driver.

这取决于您如何加载驱动程序。

If you configure the driver within you tomcat and provide it to the application then this statement is correct.

如果您在 tomcat 中配置驱动程序并将其提供给应用程序,则此语句是正确的。

But you load the driver directly within your application. Then both ways, $CATALINA_HOME/lib/(in tomcat) as well as WEB-INF/lib/(in application) should work.

但是您直接在应用程序中加载驱动程序。然后两种方式,$CATALINA_HOME/lib/(在 tomcat 中)以及WEB-INF/lib/(在应用程序中)都应该工作。

But Spring use a special way to load that class Class.forName(driverClassNameToUse, true, ClassUtils.getDefaultClassLoader())and I dont know how it behave for files in $CATALINA_HOME/lib/so I would try to put the files in your application WEB-INF/lib/folder

但是 Spring 使用一种特殊的方式来加载该类Class.forName(driverClassNameToUse, true, ClassUtils.getDefaultClassLoader()),我不知道它对文件的行为方式,$CATALINA_HOME/lib/所以我会尝试将文件放在您的应用程序WEB-INF/lib/文件夹中