Creating a JAR file that contains other library files

I want to create one JAR executable that contains other JAR libraries. But when I execute the JAR, it gives an error of the problem with the class path.

How to solve a class problem?

+6
source share
2 answers

I think you can try it like this:

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

First we need to edit our manifest file so that we can specify our path class and main class for example:

 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 as follows:

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

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

 java -jar test.jar 

This is my decision. Hope this gives you something useful ...

+9
source

To do this, you must use third-party libraries. For example, OneJar . You will need to create the final JAR file using the OneJar tool (e.g. Ant ) instead of the standard JRE .

When you run such a JAR file, the OneJar class of service is launched instead of yours. This class then loads the JAR files packaged inside, as well as your classes, and runs your main class.

Already have similar questions and answers. For example: stack overflow question. The easiest way to merge a release into a single JAR file .

0
source

Source: https://habr.com/ru/post/906181/


All Articles