Yushantha chathuranga
Yushantha chathuranga

Reputation: 19

run .jar file which has dependency with another .jar file in command line

I have a .jar file(insertdata.jar) which has a dependency with postgresql-42.2.23.jar.I have included main class in the Manifest file also. Currently both of these files are located inside C:\Users\yush\Desktop\java directory. I have tried to run insertdata1.jar file with following command in the command line.

java -jar C:\Users\yush\Desktop\java\insertdata1.jar

with this command I'm getting following error, which seems it is unable to access postgresql-42.2.23.jar classes.

java.lang.ClassNotFoundException: org.postgresql.Driver

My question is how do I run insertdata1.jar file in the command line with postgresql-42.2.23.jar?

Upvotes: 0

Views: 1221

Answers (2)

LuisDeveloper
LuisDeveloper

Reputation: 1

Ok, I had a similar issue, although I didn't use the -jar option. Is your jar file an autorunnable jar? (a jar with a manifest file, telling the classpath, and telling which one is the "main" class)

If that's the case, check if the manifest has the rightfully named variable (Class-Path) and check if you have miswritten any of the paths, separated by whitespaces (baeldung tutorial about manifest files)

If you haven't written a custom manifest, then you should drop the "-jar" option, and take a look at the following part of my explanation:

General case:(assuming you have your jars in two different folders):

  • lets assume jar1 has no dependencies, and jar2 depends on jar1.

The syntax would be:

java path/to/jar2/jar2.jar;path/to/jar1/jar1.jar (whitespace) packageNameOf.jar2.mainclass/MainClassName

(or in your case, java C:\Users\yush\Desktop\java\insertdata1.jar;path/to/your/postgreDriver/postgresql-42.2.23.jar mainPackage/Main )

what if we just want to run java over a .class that makes reference to an external jar?

java path/to/jar2/;path/to/jar1/jar1.jar packageNameOf.jar2.mainclass/MainClassName

Upvotes: 0

PrasadU
PrasadU

Reputation: 2438

2 options for you

  1. change your invocation java -cp <class-path-of-all-required-jars> <Main.class>

  2. build uber/fat jar - combines all individual jars. then you can so your -jar thing

Upvotes: 1

Related Questions