include external jar when running java -jar
Asked Answered
M

2

42

From my readings, when you execute a command as follows:

java -jar foo.jar

Then the main classpath is ignored, and the classpath is taken from the manifest file.

Further, the classpath declared on the command line is also ignored. So in:

java -classpath /usr/local/jar/foobar.jar -jar foo.jar

/usr/local/jar/foobar.jar is ignored.

Lastly, I have read that the manifest file can only contain relative paths, within the jar file.

So, how do you include absolute paths to external jars, that are present on the system, but not in the jar file being executed?

Maldonado answered 26/5, 2010 at 4:48 Comment(1)
"Note: The Class-Path header points to classes or JAR files on the local network, not JAR files within the JAR file or classes accessible over Internet protocols. To load classes in JAR files within a JAR file into the class path, you must write custom code to load those classes. For example, if MyJar.jar contains another JAR file called MyUtils.jar, you cannot use the Class-Path header in MyJar.jar's manifest to load classes in MyUtils.jar into the class path." Source: docs.oracle.com/javase/tutorial/deployment/jar/downman.htmlDisband
S
46

Is there a reason why you are avoiding invoking the main class like

java -cp /usr/local/jar/foobar.jar:/some/other/path.jar com.your.main.classname

?

This type of invocation allows you to mix absolute paths with relative paths. Put this into a shell script or batch file to avoid having to actually type or remember the full classpath to simplify things.

Surprising answered 26/5, 2010 at 4:53 Comment(3)
+1 for this practical advise! Oracle/SUN should really deprecate the -jar option... it's a reoccurring nightmare...Zebrass
Well using -jar can be significantly easier in some scenarios, but if you find that the simplified option is giving you trouble then it always makes sense to fallback to the option that gives you total controlSurprising
I think, there should be a semicolon on Windows instead of the colon between jar paths. Please check these answers https://mcmap.net/q/45545/-including-all-the-jars-in-a-directory-within-the-java-classpath https://mcmap.net/q/46753/-execute-jar-file-with-multiple-classpath-libraries-from-command-promptKalgoorlie
H
43

You can create a folder, say lib, within the folder where you have the jar file.

Manifest.MF contents can be:

Main-Class: com.mastergaurav.test.app.MainClass
Class-Path: lib/one.jar lib/two.jar

Folder contents:

mainFolder/
   * lib/one.jar
   * lib/two.jar
   * my-main.jar

To execute:

java -jar my-main.jar
Hightension answered 26/5, 2010 at 4:52 Comment(4)
btw, if you execute java -cp /addtional/class/path -jar abcd.jar, the addtional classpath will not be ignored.Hightension
You have absolutely no idea how long I've searched for this. Thanks.Uncounted
How do I set the Class-Path to a jar inside of the jar I want to compile?Disinclination
@Arin to my knowledge this is not possible without writing a custom class loader. Even then, it seems impossible based on a day and a half long spike. See AlikElzin-kilaka's comment above and bugs.java.com/bugdatabase/view_bug.do?bug_id=4648386. Of course you could explore One-Jar.Sally

© 2022 - 2024 — McMap. All rights reserved.