Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

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.

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

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

mysql-connector has been copied to $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.jar in CATALINA_HOME, in CATALINA_BASE and in both of them):

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.

My spring datasource definition is very simple:

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

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$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:4701)
    at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5204)
    at org.apache.catalina.core.StandardContext$1.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$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:4701)
    at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5204)
    at org.apache.catalina.core.StandardContext$1.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.jar is being open by Tomcat:

"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

Any further idea?


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

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?

like image 655
Javier Sedano Avatar asked Aug 30 '12 07:08

Javier Sedano


1 Answers

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:

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:

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.

Downloading the driver again did solve the problem.

Downvote for myself :-(

like image 110
Javier Sedano Avatar answered Oct 15 '22 11:10

Javier Sedano