Join the Stack Overflow Community
Stack Overflow is a community of 6.5 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

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();
        }
               }
share|improve this question

marked as duplicate by BalusC eclipse Jun 2 '16 at 15:22

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

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
up vote 17 down vote accepted

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

share|improve this answer
    
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 <scope>runtime</scope> and I'm still seeing the ClassNotFound exception. – user977505 Nov 12 '11 at 18:03

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.

share|improve this answer
    
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: maven.40175.n5.nabble.com/…. Some of commenters are simply copying resources explicitly in pom.xml – soulcheck Nov 12 '11 at 18:20

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.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.