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();
        }
               }
user977505
  • 539
  • 3
  • 9
  • 19
  • 1
    Have you also copied the mysql connector jar file in the Tomcat lib folder? – Tudor Nov 12 '11 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. – user977505 Nov 12 '11 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. – stivlo Nov 12 '11 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. – user977505 Nov 12 '11 at 17:30
  • are you using jdni to lookup datasource? – soulcheck Nov 12 '11 at 17:41

3 Answers3

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".

duffymo
  • 305,152
  • 44
  • 369
  • 561
  • 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? – stivlo Nov 12 '11 at 17:15
  • 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. – soulcheck Nov 12 '11 at 17:34
  • 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. – user977505 Nov 12 '11 at 17:35
  • "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. – duffymo Nov 12 '11 at 18:00
  • Changing the scope in the pom.xml did not help. I changed to scope of the mysql connector jar to runtime and I'm still seeing the ClassNotFound exception. – user977505 Nov 12 '11 at 18:03
  • @duffymo How did they switch exactly? Putting drivers in tomcat lib directory was the way to go with jndi datasources since at least 5.5. Asker isn't using jndi so there's no connection/pool sharing and hence putting driver in global lib directory isn't good idea. – soulcheck Nov 12 '11 at 18:15
  • Nope, you can still put JDBC driver JARs in your local WEB-INF/lib with Tomcat 6.x It changed with version 7. And Tomcat disagrees with you. Why is it a bad idea? I see no harm. – duffymo Nov 12 '11 at 21:13
  • @duffymo Regarding "Pollutes the global /lib directory", I think it could be an issue if developers are not aware that this location is being used to find their JDBC driver. For instance, someone may think they updated the JDBC driver by switching up the POM, however the old driver sits in ~tomcat/lib never being updated. Just wanted to chime in with that, appreciate you pointing this out though it helped fx my issue. – Kevin Bowersox May 26 '14 at 18:25
  • 1
    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. – SexyBeast Oct 23 '15 at 17:43
  • After days looking for solutions, this is the only one that works for me. Thanks @duffymo, I wish I could give more than 1 upvote – fruqi Jan 26 '17 at 08:42
  • So kind of you to say; I'm glad to help. – duffymo Jan 26 '17 at 10:13
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.

soulcheck
  • 36,297
  • 6
  • 91
  • 90
  • How do I do that? By changing the scope of the maven dependency for mysql connector jar or manually copying it? – user977505 Nov 12 '11 at 18:06
  • I'm not using JNDI. I updated my post on how I go about loading the Driver. – user977505 Nov 12 '11 at 18:08
  • 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. – soulcheck Nov 12 '11 at 18:16
  • It's the same problem here: http://maven.40175.n5.nabble.com/wtp-projects-dependencies-don-t-show-up-in-embedded-tomcat-WEB-INF-lib-directory-td139868.html. Some of commenters are simply copying resources explicitly in pom.xml – soulcheck Nov 12 '11 at 18:20
-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.

Ant3G
  • 1