java.lang.ClassNotFoundException: org.postgresql.Driver in Terminal
Asked Answered
N

2

0

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

Nasal answered 16/8, 2022 at 23:37 Comment(14)
The class is not on your classpath.Newbold
I tried this, but it did not work: java -classpath org.postgresql:postgresql:42.3.1 -jar target/posthogdata-1.0-SNAPSHOT.jarNasal
That does not look like correct usage of -cp to me. From the Java spec usage of -cp or -classpath is as follaws -cp classpath Specifies a list of directories, JAR files, and ZIP archives to search for class files. Separate class path entries with semicolons (;). Specifying -classpath or -cp overrides any setting of the CLASSPATH environment variable. Do you really have a file named org.postgresql:postgresql:42.3.1? If so then it is not in the correct location relative to where the command is being run from.Planarian
@Planarian I have org.postgresql:postgresql:42.3.1 jar in the pom and therefore in the External Libraries. I fail to understand why this is not part of the classpath automatically.Nasal
The name of the JAR file is postgresql-jdbc-42.3.1.jar so the beginning of the java command should be: java -cp postgresql-jdbc-42.3.1.jar (assuming that the JAR file is in the same directory that you are issuing the command from)Mccahill
@Mccahill I used this like you suggested java -cp postgresql-jdbc-42.3.1.jar -jar target/posthogdata-1.0-SNAPSHOT.jar same error. How do I check the directory of a dependent jar?Nasal
Just search for the file postgresql-jdbc-42.3.1.jar on your computer. Or is that file not on your computer?Mccahill
Its here: /Users/meuser/.m2/repository/org/postgresql/postgresql/42.3.1. I am so confused why is java not picking this up? Other dependencies are there as well. Tried this as well, same error: java -cp /Users/meuser/.m2/repository/org/postgresql/postgresql/42.3.1/postgresql-42.3.1.jar -jar -jar target/posthogdata-1.0-SNAPSHOT.jar Nasal
If you use java -jar the -cp option is ignored. This is documented. Your .jar file needs to mention a relative path to the Postgres driver jar in the Class-Path entry in its manifest. NB The Class.forName() line hasn't been needed since 2006.Halverson
@Halverson Here it says you can use -cp with -jar. I do not have a manifest file for the jar, where does it go in the project structure?Nasal
Okay, so maven would not create the manifest for me so I went ahead and created it using IntelliJ I added the following, but still same error: Manifest-Version: 1.0 Class-Path: postgresql-42.3.1.jar Main-Class: luminai.data.DataProcessorNasal
No it doesn't. Here it says exactly the opposite: "when you use this option, the JAR file is the source of all user classes, and other user class path settings are ignored". Re your new manifest, is postgresql-32.3.1.jar in the same directory as your JAR file? Because that's what your manifest entry says. If it is say in a lib subdirectory the manifest entry for it needs to say so.Halverson
Thanks for quoting! Makes sense, don't know then why it's being used all over StackOveflow. Re the JAR file it's a dependency that is part of my pom.xml. It's definitely not there in any of my project folders, but on my local machine here: /Users/meuser/.m2/repository/org/postgresql/postgresql/42.3.1 How do I get a dependent jar file to a directory in my project? While my jar file is in the target folderNasal
Because people apparently cannot read, even when they find the correct place, and cite things that don't say what they are claimed to say. I have no idea about Maven but it's doable. In one of my few Maven projects I have <dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.11</version><type>jar</type></dependency>.Halverson
N
0

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
Nasal answered 17/8, 2022 at 23:27 Comment(0)
C
0

Usually reloading Gradle / Maven from IntellijIdea solves issue.

Camion answered 10/5 at 4:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.