include external jar when running java -jar

老子叫甜甜 提交于 2019-12-17 06:38:38

问题


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?


回答1:


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.




回答2:


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


来源:https://stackoverflow.com/questions/2910115/include-external-jar-when-running-java-jar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!