Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven - java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

I have a Java app based on Maven, and want to connect to MySQL server.

My pom has:

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.17</version>
    <type>jar</type>
    <scope>runtime</scope>
</dependency>

With runtime, as I want to connect to MySQL server at runtime - have also tried compile and provided, but does not work.

The SQL code is standard:

String dbClass = "com.mysql.jdbc.Driver";

Class.forName(dbClass);
Connection connection = DriverManager.getConnection(dbUrl,
    username, password);
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(query);
while (resultSet.next()) {
    String tableName = resultSet.getString(1);
    System.out.println("Table name : " + tableName);
}

When I run this from Eclipse, it works fine and prints table names.

However, from maven, the generated SNAPSHOT always gives an error when executed via >java -jar target\File.jar after running mvn clean install.

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

What am I missing here to get the maven build to work? Running mvn clean install gives no error and builds fine. It is only when executing the SNAPSHOT exe the error happens.

The MySQL jar is in my .m2 repo, and I tried adding it explicitly via mvn command line, but says it already exists.

like image 684
pokero Avatar asked Mar 24 '15 23:03

pokero


People also ask

How do I add mysql connector jar to classpath?

After extracting the distribution archive, you can install the driver by placing MySQL-connector-java-version-bin. jar in your classpath, either by adding the full path to it to your classpath environment variable or by directly specifying it with the command line switch -cp when starting the JVM.


2 Answers

Since you are running the project by "java -jar" and also you have dependencies, so, you have to use two maven plugins. The first one to copy dependencies into a folder inside target folder (e.g. lib/) while packaging and the second one for specifying the classpath which should be same as the first one(lib/). I had the same problem and here is what I did:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
                <execution>
                    <id>copy</id>
                    <phase>package</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>
                            ${project.build.directory}/lib
                        </outputDirectory>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>lib/</classpathPrefix>

                  <mainClass>com.tihoo.crawler.Application</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
    </plugins>
</build>
like image 130
Khosro Makari Avatar answered Sep 20 '22 22:09

Khosro Makari


The answer is here - How can I create an executable JAR with dependencies using Maven?

I needed to build an uber pom, using the answer in the link above - this builds in the dependencies (in this case the mysql jar file) into a single SNAPSHOT jar file.

Just make sure you run it with mvn clean compile assembly:single (not the usual mvn clean package or whatever.

like image 43
pokero Avatar answered Sep 17 '22 22:09

pokero