0

I have a project that connects to a postgresql database (version 12) with java 1.8.

When running the project with mvn java:exec, there is no problem.

However, when packaging (mvn package) the project into a jar and executing the jar with java -jar jarfile.jar, it returns me the following error:

java.sql.SQLException: No suitable driver found for jdbc:postgresql://localhost:5432/ulttra

I have the dependency in pom.xml

http://maven.apache.org/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>com.aptar.ultimate-traceability</groupId>
<artifactId>ut-digital-twin</artifactId>
<packaging>jar</packaging>
<version>0.3.0</version>
<name>UT-digital-twin</name>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.glassfish.jersey</groupId>
            <artifactId>jersey-bom</artifactId>
            <version>${jersey.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>
    <!-- https://mvnrepository.com/artifact/com.microsoft.sqlserver/mssql-jdbc -->
    <dependency>
        <groupId>com.microsoft.sqlserver</groupId>
        <artifactId>mssql-jdbc</artifactId>
        <version>6.2.2.jre8</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-grizzly2-http</artifactId>
    </dependency>
    <dependency>
        <groupId>org.json</groupId>
        <artifactId>json</artifactId>
        <version>20190722</version>
    </dependency>
    <dependency>
        <groupId>com.google.zxing</groupId>
        <artifactId>core</artifactId>
        <version>3.4.0</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.inject</groupId>
        <artifactId>jersey-hk2</artifactId>
    </dependency>
    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>42.2.10</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>2.13.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>2.13.1</version>
    </dependency>

    <!-- uncomment this to get JSON support: <dependency> <groupId>org.glassfish.jersey.media</groupId> 
        <artifactId>jersey-media-json-binding</artifactId> </dependency> -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.48</version>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.5.1</version>
            <inherited>true</inherited>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.2.1</version>
            <executions>
                <execution>
                    <goals>
                        <goal>java</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <mainClass>com.example.Main</mainClass>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>3.0.0</version>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <archive>
                    <manifest>
                        <mainClass>com.example.Main</mainClass>
                    </manifest>
                </archive>
            </configuration>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>

</build>

<properties>
    <jersey.version>2.29.1</jersey.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

My postgresconnector class:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class PostgreConnector {
    private Connection c;
    Logger log = LogManager.getLogger("com.aptar.simulator");
    private static PostgreConnector instance = new PostgreConnector();


private PostgreConnector() {

}

/**
 * 
 * @return The instance of Database
 */
public static PostgreConnector getInstance() {
    return instance;
}

/**
 * Prepare the connection to the database
 */
public void prepare() {
    try {
        this.c = DriverManager.getConnection(this.url, this.user, this.password);
    } catch (SQLException e) {
        log.error("Preparing DB connection failed", e);
    }
}

The error appears on prepare method.

How can I fix this ?

Florian Castelain
  • 1,524
  • 12
  • 39
  • Add the complete pom. – Jens Mar 22 '20 at 08:04
  • @Jens Complete pom included. – Florian Castelain Mar 22 '20 at 08:04
  • Are you sure you ran the `ut-digital-twin-0.3.0-jar-with-dependencies.jar` instead of the `ut-digital-twin-0.3.0.jar` ? – J Fabian Meier Mar 22 '20 at 09:07
  • 2
    You are running in an application server, automatic driver loading doesn't work in this context. Either load the driver explicit using `Class.forName("org.postgresql.Driver")`, or (better) switch to using a `DataSource` backed by a connection pool; in the course of its configuration you usually must specify the driver anyway. – Mark Rotteveel Mar 22 '20 at 09:58

0 Answers0