Home > Blockchain >  java.lang.ClassNotFoundException: org.postgresql.Driver in Terminal
java.lang.ClassNotFoundException: org.postgresql.Driver in Terminal

Time:08-20

I have seen a LOT of questions on the same topic, but honestly nothing has worked after few hours of debugging here. I have a java with maven project that calls postgres DB to read and write data. The code builds and runs fine on IntelliJ. I am trying to run it on terminal (so that I can use determine the command that works and use it for my dockerfile). For compiling:

$mvn clean install

For running:

$java -jar target/posthogdata-1.0-SNAPSHOT.jar  

I see the following error:

java.lang.ClassNotFoundException: org.postgresql.Driver
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
    at java.base/java.lang.Class.forName0(Native Method)
    at java.base/java.lang.Class.forName(Class.java:383)
    at java.base/java.lang.Class.forName(Class.java:376)
    at luminai.data.PostgresClient.createConnection(PostgresClient.java:19)
    at luminai.data.DataProcessor.main(DataProcessor.java:38)

The code is breaking at this line:

Class.forName("org.postgresql.Driver");

I am confused as to whats the problem at this point. I have the right dependency:

<dependency>
   <groupId>org.postgresql</groupId>
   <artifactId>postgresql</artifactId>
   <version>42.3.1</version>
</dependency>

And this is my plugin:

<build>
    <plugins>
        <plugin>
            <!-- Build an executable JAR -->
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>3.1.0</version>
            <configuration>
                <archive>
                   <manifest>
                      <addClasspath>true</addClasspath>
                      <classpathPrefix>lib/</classpathPrefix>
                      <mainClass>luminai.data.DataProcessor</mainClass>
                   </manifest>
                </archive>
            </configuration>
        </plugin>
    </plugins>
</build>

EDIT:

Commands I have tried:

$java -classpath org.postgresql:postgresql:42.3.1 -jar target/posthogdata-1.0-SNAPSHOT.jar

$java -classpath org.postgresql:postgresql:42.3.1:* -jar target/posthogdata-1.0-SNAPSHOT.jar

$java -cp .:org.postgresql:postgresql:42.3.1 -jar target/posthogdata-1.0-SNAPSHOT.jar

Why is the postgres jar not part of the classpath automatically, when it is there in the pom and list of External Libraries?

enter image description here

Here is the structure of my project, I am running the command from >posthogdata enter image description here

CodePudding user response:

I found the fix. As per the comments and the error itself, the Postgres driver jar was not in the classpath. After certain tries, I changed my pom plugin to include my project's jar in a way that combines all the other dependent jars into it. I guess IntelliJ was doing that for me out of the box.

Changed plugin to:

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>luminai.data.Main</mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
            </plugin>
        </plugins>
    </build>

And ran the following commands:

$ mvn clean compile assembly:single
$ java -jar target/posthogdata-1.0-SNAPSHOT-jar-with-dependencies.jar
  • Related