Java:使用嵌入的依赖jar创建jar可执行文件 [英] Java : create jar executable with dependant jars embedded
问题描述
我用清单创建了一个可执行的JAR:
I have created an executable JAR with the manifest:
Manifest-Version: 1.0
Main-Class: MyClass
Class-Path: lib/ext.jar
我可以成功运行此JAR我在.jar创建的文件夹中有依赖 ext.jar 的文件夹 lib 。
I can successfully run this JAR if I have folder lib with dependent ext.jar in the folder of .jar created.
我想要在我的可执行JAR中嵌入 ext.jar ,以便在我想要的任何地方启动它,而无需创建lib文件夹。这将允许JAR文件自包含,包括其所有依赖项。
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?
推荐答案
默认的Java类加载器无法在jar文件中找到嵌入式jar。您需要将Java程序设置为使用可以处理嵌入式jar文件的类加载器。据我所知,我没有在任何开源库中看到过,所以你可能需要自己编写一个。
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.
或者,你可以提取所有jar文件并结合使用将内容放入一个jar文件中。
Alternatively, you can extract all the jar files and combine the the content into a single jar file.
这篇关于Java:使用嵌入的依赖jar创建jar可执行文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!