Java : create jar executable with dependant jars embedded

北城以北 提交于 2019-11-26 18:37:03

问题


I have created an executable JAR with the manifest:

Manifest-Version: 1.0
Main-Class: MyClass
Class-Path: lib/ext.jar

I can successfully run this JAR if I have folder lib with dependent ext.jar in the folder of .jar created.

I would like to embed ext.jar in my executable JAR to launch it anywhere I want, without creating lib folder. This would allow the JAR file to be self-contained, including all of its dependencies.

Is there any way to do this?


回答1:


The default Java class loader cannot find an embedded jar inside a jar file. You will need to set your Java program to use a class loader which can deal with embedded jar files. As fas as I know, I have not seen one in any opensource libraries so you may need to write one yourself.

Alternatively, you can extract all the jar files and combine the the content into a single jar file.

Clean way to combine multiple jars? Preferably using Ant




回答2:


Java doesn't have out-of-the-box support for embedding a jar within a jar. It can be achieved by writing a custom class loader, or using a tool that's already been written for this purpose: Fat Jar.



来源:https://stackoverflow.com/questions/8312909/java-create-jar-executable-with-dependant-jars-embedded

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