Creating a JAR file which contains other library files

痞子三分冷 提交于 2019-11-28 07:04:37
Tony

I think you can try it like this;

Here is a simple example for you question. First, we assume we have a project directory like D:\javademo. In this working directory we then create a main class HelloWorld.java and thtat contains our other JAR files, like commons-lang.jar. Now, we must archive our main classes HelloWorld and commons-lang.jar into test.jar file.

First we must edit our manifest file so that we can specify our class-path and main-class like this:

Manifest-Version: 1.0 
Created-By: tony example
Class-Path: test.jar commons-lang.jar
Main-Class: org.tony.java.HelloWorld

We named this file test.mf. Now we use the jar command to generate our JAR file like this:

jar -cvfm test.jar test.mf -C ./ .

Then it will generate the JAR file test.jar. You can use this command to run this main class using java command:

java -jar test.jar

That is my solution. I hope it give you something helpful...

Wizart

You should use third-party libraries for it. For example, OneJar. You'll have to build the final JAR file using the OneJar tool (like Ant task) instead of standard JRE's tools.

On running such a JAR file, OneJar's service class is launched instead of yours. This class then loads JAR files packed inside, as well as your classes, and run your main class.

There are similar questions and answers already. For example: Stack Overflow question Easiest way to merge a release into one JAR file.

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