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?
Here is the structure of my project, I am running the command from >posthogdata
-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 namedorg.postgresql:postgresql:42.3.1
? If so then it is not in the correct location relative to where the command is being run from. – Planarianpostgresql-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) – Mccahilljava -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? – Nasalpostgresql-jdbc-42.3.1.jar
on your computer. Or is that file not on your computer? – Mccahill/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
– Nasaljava -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 TheClass.forName()
line hasn't been needed since 2006. – HalversonManifest-Version: 1.0 Class-Path: postgresql-42.3.1.jar Main-Class: luminai.data.DataProcessor
– Nasalpostgresql-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 alib
subdirectory the manifest entry for it needs to say so. – Halverson/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 thetarget
folder – Nasal<dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.11</version><type>jar</type></dependency>
. – Halverson