Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver during Runtime (eclipse / maven /tomcat) [duplicate]

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();
        }
               }
like image 210
user977505 Avatar asked Nov 12 '11 17:11

user977505


2 Answers

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

like image 170
duffymo Avatar answered Oct 05 '22 17:10

duffymo


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.

like image 32
soulcheck Avatar answered Oct 05 '22 15:10

soulcheck