8

I'm getting this error while running tomcat 'java.lang.ClassNotFoundException: com.mysql.jdbc.Driver'. I'm using a combination of Eclipse (Indigo, J2EE version) / Maven (m2e-wtp) / Tomcat 7.0. I've included this dependency in my pom file for my web application (build from scratch).

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

I do not get any compilation errors in the 'Problems' view but when I run the Tomcat server from the 'Servers' view, I get these errors. It clearly indicates that Tomcat is unable to find the Class and it is classpath configuration error and I was hoping that maven would take care of this.

I looked at other issues related to 'java.lang.ClassNotFoundException: com.mysql.jdbc.Driver' but weren't of much help.

I would greatly appreciate any help.

/** This is how I load the Driver */

static {
        DriverAdapterCPDS cpds_Customer = new DriverAdapterCPDS();
        try {                        cpds_Customer.setDriver(productConfig.getProperty("dbcp.connection.customer.driver_class"));

        } catch (ClassNotFoundException e) {
            // log.error("setDriver Exception " + e);
            e.printStackTrace();
        }
               }
5
  • 1
    Have you also copied the mysql connector jar file in the Tomcat lib folder? Commented Nov 12, 2011 at 17:05
  • Thanks for the fast response. I did not try it because I was hoping that maven would take care of all the jar dependencies by copying the necessary jar to WebContent/WEB-INF/lib or where ever it puts them. Am I suppose to do that explicitly? I'll try and update the thread. Commented Nov 12, 2011 at 17:11
  • Tudor and duffymo are suggesting you to copy the driver in tomcat's lib directory ($CATALINA_HOME/lib), not in the web application lib directory. Commented Nov 12, 2011 at 17:18
  • Yes, now copied it and I don't see the exception anymore. I knew that it was one of the solutions but I was trying to figure out how I could configure it through maven to take care of it w/ out any manual configuration. Appreciate that help. Commented Nov 12, 2011 at 17:30
  • are you using jdni to lookup datasource? Commented Nov 12, 2011 at 17:41

3 Answers 3

19

Tomcat 7 requires that JDBC driver JARs must go in its /lib directory:

http://tomcat.apache.org/tomcat-7.0-doc/jndi-datasource-examples-howto.html

Search for the word "forget".

Sign up to request clarification or add additional context in comments.

11 Comments

so what about the scope in pom.xml? the default compile scope will package it in the war as well, would it be a problem?
it's only true when you use jdni resource lookup to find datasources, which might not be asker's case. If it's not it's actually a bad advice and should be avoided as it pollutes global lib directory with libraries needed only by one webapp and aren't deleted in case of undeploy.
That was very critical. I did not copy it explicitly in the past when working w/ other versions of Tomcat and I overlooked it for this version (Tomcat 7.0). I'll also try changing the scope in pom and see if that helps avoiding to copy explicitly into /lib directory.
"Pollutes the global /lib directory"? If such pollution was harmful, I doubt that the folks who write Tomcat would have made the switch. It's a good thing in the sense that apps deployed on a single instance of Tomcat are likely to share a data source as well; this arrangement makes sure that everyone has access to the proper driver. It's a bad thing if different apps need different versions of the driver, but that's usually not the case if the database is shared.
This fixes the error. For those who are doing it via Dynamic Web Project from Eclipse and don't know the value of $CATALINA_HOME, do a find to locate catalina.sh (for me it was /Library/Tomcat/apache-tomcat-8.0.28/bin/catalina.sh), then invoke that script as catalina.sh version, and in the output you will see the value of $CATALINA_HOME. Under the lib folder in that directory, copy the mysql jar.
|
3

Make sure the driver actually gets copied to your webapp WEB-INF/lib directory and to wtp deploy dir (something like /.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/ in your workspace).

I find maven-wtp integration a bit worse than perfect as i stumble upon this problem very often.

4 Comments

How do I do that? By changing the scope of the maven dependency for mysql connector jar or manually copying it?
I'm not using JNDI. I updated my post on how I go about loading the Driver.
I only managed to do it by manually copying them. Maybe there's a better solution. If so i'd be interested to know it.
It's the same problem here: maven.40175.n5.nabble.com/…. Some of commenters are simply copying resources explicitly in pom.xml
-1

for this error: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

you need to "Import com.mysql.jdbc.Driver;" even if its not used till app running.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.