简体   繁体   中英

How to diagnose a ClassNotFound exception for MySQL JDBC driver

My webapp is failing to get a connection to MySQL, failing with the error:

Caused by: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

Now the mysql connector jar is clearly in my classpath: mysql-connector-java-5.0.7-bin.jar, in the WEB-INF/lib directory.

So it doesn't make sense that it's "not found." I'm guessing it must be failing some static initialization.

Is there any way to diagnose this problem?

While I didn't come up with any good diagnostic utilities, aside from the stack trace, I did realize what was going on. Naturally the stacktrace has the clue:

Cannot load JDBC driver class 'com.mysql.jdbc.Driver'
    at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1136)
    at org.apache.tomcat.dbcp.dbcp.BasicDataSource.getConnection(BasicDataSource.java:880)

The DataSource was defined in a context.xml file within the web application, which means that the driver was being loaded by Tomcat using the parent classloader to the web app. That classloader doesn't have access to the application's classpath.

The solution is to drop the mysql jar in Tomcat's shared library directory.

A different solution is to embed the DataSource within the app instead of using Tomcat's DataSource management, thus allowing a stock Tomcat distribution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM