1

I have a Maven project in Eclipse (using ubuntu) with a class dbtest that access my SQL table in MySQL where I am using Apache Maven 3.3.9 and Java 1.8.0_181.

When I am running and compiling the code using Eclipse it is working good (I do have a database called acm with "root" as my username and password) but when I create a Jar using Maven command:

mvn clean install

and I compile it I get the following error:

java -cp target/maven-1.jar test.dbtest
start
Class-start
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/acm
End main

I have followed https://stackoverflow.com/questions/1074869/find-oracle-jdbc- and tried many options such as Peter Enis answer or adding compile scope to the dependency and none of them have solved the problem. Moreover, I have also downloaded the mysql-connector-java jar (I have tried many versions) and added it to the library path, and it won't help, so I added the extracted jar to the path and the problem remains.

Below are the dbtest class and my POM file.

package test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class dbtest  
{
public static void main(String[] args) throws SQLException 
{
    try
    {
    System.out.println("start");
    //Class.forName("com.mysql.jdbc.Driver");//.newInstance();
    System.out.println("Class-start");
    Connection  connection = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/acm", "root", "root");
    System.out.println("connection");
    Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
    System.out.println("statement");
    String sql = "SELECT * FROM article_102 limit 4";
    ResultSet rs = statement.executeQuery(sql);
    System.out.println("article_id | publication_id");
    while (rs.next())
        System.out.println(rs.getString("article_id") + ", " + rs.getString("publication_id"));
    statement.close();
    connection.close();
    }//try
            catch(Exception e){ System. out.println(e.toString());}
    System.out.println("End main");
    }//main

}

<?xml version="1.0" encoding="UTF-8"?>

<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>maven</groupId>
  <artifactId>maven</artifactId>
  <version>1</version>

  <name>maven</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
       <dependency>
       <groupId>mysql</groupId>
       <artifactId>mysql-connector-java</artifactId>
       <version>5.1.6</version>
    </dependency>

  </dependencies>

  <build>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.0.0</version>
        </plugin>
        <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
        <plugin>
      <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.7.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.20.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-jar-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

Comment, I am querious if I need this line Class.forName("com.mysql.jdbc.Driver");//.newInstance(); I have left it as a comment (and when I use it I just get that java.lang.ClassNotFoundException: com.mysql.jdbc.Driver) because some say I should and others say it won't change.

Or Raz
  • 39
  • 2
  • 11

2 Answers2

0

Class.forName("com.mysql.jdbc.Driver").newInstance(); is needed to register the driver as explained in 6.1 Connecting to MySQL Using the JDBC DriverManager Interface.

The error java.lang.ClassNotFoundException: com.mysql.jdbc.Driver means that the JAR file containing com.mysql.jdbc.Driver class is not present on the classpath. The easiest way to fix is to change the -cp value:

java -cp path/to/mysql-connector-java.jar:target/maven-1.jar test.dbtest

There are other ways to do it e.g. use MANIFEST.MF with java -jar or build an uber jar using Maven Shade Plugin.

Karol Dowbecki
  • 43,645
  • 9
  • 78
  • 111
  • java -cp path/to/mysql-connector-java.jar;target/maven-1.jar test.dbtest won't work, I have changed the semicolon (;) to a colon (:) and it is working. But how can I add the com.mysql.jdbc.Driver class to my classpath? your solution is more like a bandaid... – Or Raz Nov 05 '18 at 18:51
  • @OrRaz you need to package a runnable jar with all dependencies. There are few ways different ways to do it e.g. [this answer](https://stackoverflow.com/a/26527073/1602555). – Karol Dowbecki Nov 05 '18 at 20:10
  • It is working! Although now `mvn clean install` takes much more time (30 seconds in comparison to 4 seconds), not only for the first time of running. Is it ok? – Or Raz Nov 09 '18 at 18:59
0

add this plugin in your pom and execute

java -jar your_project-jar-with-dependencies.jar

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
        <executions>
            <execution>
                <phase>package</phase>
                <goals>
                    <goal>single</goal>
                </goals>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>
                                test.dbtest
                            </mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
            </execution>
        </executions>
    </plugin>
Gnk
  • 645
  • 4
  • 11
  • After editing, your command doesn't work in the beginning because I had to specify the main class, therefore I have used https://stackoverflow.com/questions/9689793/cant-execute-jar-file-no-main-manifest-attribute Then, your command is working but I get the exception java.lang.ClassNotFoundException: com.mysql.jdbc.Driver – Or Raz Nov 05 '18 at 19:38
  • did you add mysql dependency? – Gnk Nov 05 '18 at 19:44
  • Which mysql dependency? As I have mentioned above my POM file I have mysql mysql-connector-java 5.1.6 – Or Raz Nov 05 '18 at 19:49
  • I think you are executing the wrong jar, you have to execute jar-with-dependencies – Gnk Nov 05 '18 at 19:52