JPA, Maven and MySQL. Configuration error. Class [com.mysql.jdbc.Driver] not found

10,137

Solution 1

Simply add a dependency to the MySQL Java connector with a runtime scope:

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.21</version>
    <scope>runtime</scope>
</dependency>

Solution 2

It's probably because you have the mysql-dependency (mysql-connector-java) twice in your pom. Once with scope test and once with the default scope. Removing the one with test scope will hopefully solve your classnotfound.

Share:
10,137

Related videos on Youtube

Agung Prasetyo
Author by

Agung Prasetyo

There are no secret ingredients, just belief. Please visit my vlog here.

Updated on June 04, 2022

Comments

  • Agung Prasetyo
    Agung Prasetyo almost 2 years

    SOLVED: just put mysql-connector-java-5.1.x.jar inside JAVA_HOME\jre\lib\ext.

    hello all, i'm just starting build a demo apps that used maven, mysql and JPA and this is my code : pom.xml :

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.ee6.book</groupId>
    <artifactId>chapter02</artifactId>
    <version>2.0</version>
    <name>Chapter 02 - JPA</name>
    
    
    <dependencies>
        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>javax.persistence</artifactId>
            <version>2.0.0</version>
        </dependency>
        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>eclipselink</artifactId>
            <version>2.0.1</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.6</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.6</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.8.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <inherited>true</inherited>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
    

    and my persistence.xml :

    <persistence-unit name="chapter02PU" transaction-type="RESOURCE_LOCAL">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <class>org.ee6.book.chapter02.Book</class>
    <properties>
        <property name="eclipselink.target-database" value="MYSQL"/>
        <property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
        <property name="eclipselink.logging.level" value="SEVERE"/>
        <property name="eclipselink.jdbc.driver" value="com.mysql.jdbc.Driver"/>
        <property name="eclipselink.jdbc.url" value="jdbc:mysql://localhost:3306/chapter02DB"/>
        <property name="eclipselink.jdbc.user" value="root"/>
        <property name="eclipselink.jdbc.password" value="xyz"/>    
    </properties>
    

    when execute mvn compile it just fine, but when i try to execute main class

    mvn exec:java -Dexec.mainClass="org.ee6.book.chapter02.Main"
    

    I've got error like :

        [EL Severe]: 2011-06-03 00:23:27.89--ServerSession(26373776)--Local Exception Stack:
    Exception [EclipseLink-4003] (Eclipse Persistence Services - 2.0.1.v20100213-r6600): org.eclipse.persistence.exceptions.DatabaseException
    Exception Description: Configuration error.  Class [com.mysql.jdbc.Driver] not found.
            at org.eclipse.persistence.exceptions.DatabaseException.configurationErrorClassNotFound(DatabaseException.java:82)
            at org.eclipse.persistence.sessions.DefaultConnector.loadDriverClass(DefaultConnector.java:267)
            at org.eclipse.persistence.sessions.DefaultConnector.connect(DefaultConnector.java:85)
            at org.eclipse.persistence.sessions.DatasourceLogin.connectToDatasource(DatasourceLogin.java:162)
            at org.eclipse.persistence.internal.databaseaccess.DatasourceAccessor.connectInternal(DatasourceAccessor.java:327)
            at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.connectInternal(DatabaseAccessor.java:295)
            at org.eclipse.persistence.internal.databaseaccess.DatasourceAccessor.connect(DatasourceAccessor.java:415)
            at org.eclipse.persistence.sessions.server.ConnectionPool.buildConnection(ConnectionPool.java:155)
            at org.eclipse.persistence.sessions.server.ConnectionPool.startUp(ConnectionPool.java:433)
            at org.eclipse.persistence.sessions.server.ServerSession.connect(ServerSession.java:495)
            at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.login(DatabaseSessionImpl.java:632)
            at org.eclipse.persistence.internal.jpa.EntityManagerFactoryProvider.login(EntityManagerFactoryProvider.java:230)
            at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:369)
            at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.getServerSession(EntityManagerFactoryImpl.java:151)
            at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManagerImpl(EntityManagerFactoryImpl.java:207)
            at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:195)
            at org.ee6.book.chapter02.Main.main(Main.java:20)
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
            at java.lang.reflect.Method.invoke(Method.java:597)
            at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:291)
            at java.lang.Thread.run(Thread.java:662)
    

    I'm using maven 2.2.1. Any help would be greatly appreciated! Thanks

  • Agung Prasetyo
    Agung Prasetyo almost 13 years
    sorry. the problem still exist.