Package and use the JAR library in JAR

Source: Internet
Author: User

Package and use the JAR library in JAR

I wonder if you have such a requirement when writing a Java program: Package the project that references other third-party JAR libraries into a JAR file for running. that is to say, the packaged JAR file contains the third-party JAR files you have referenced and synthesized a JAR package. In this way, you only need to double-click the package in Windows or enter java-jar yourjarfile in the Linux terminal. jar to facilitate the operation. the first common practice is to use the Class-Path option in the META-INF/MANIFEST in the packaged JAR file, like this:

Manifest-Version: 1.0Created-By: 1.7.0_06-b24 (Oracle Corporation)Main-Class: YOUR.MAIN.CLASS.NAME.HEREClass-Path: lib1.jar lib2.jar lib3.jar

However, when running this JAR package, you still need to put the referenced lib1.jar, lib2.jar, and lib3.jar into the JAR file directory you have packaged. even if you package it into your JAR package, it cannot run independently.

In the past, the problem of packaging like this has always plagued me. Isn't it possible for Java to run a single file like an exe package program?

Until today again encountered this problem, GOOGLE found a good solution. is to use a third-party toolkit One-Jar. Corresponding: http://sourceforge.net/projects/one-jar/

Using One-Jar is very simple. you only need a few simple steps, but before using One-Jar, you need to package your project into a jar package by using the JAR command, excluding the third-party JAR package, and then perform the following operations:

1. Create a working directory named "root", which contains two subdirectories named "main" and "lib;

2. here, root/main is used to store the JAR file you have packaged and renamed it main. jar. do not include any referenced JAR package when packaging your class file. the corresponding root/lib is used to store the third-party JAR packages you reference.

3. download the latest One-Jar package from the One-Jar website. decompress the package to the root directory, and merge the corresponding duplicate directories. remove the "src" directory. (note the hidden files when you unpack One-Jar ).

4. Create a new file named boot-manifest.mf in the root directory with the following content:

Manifest-Version: 1.0Main-Class: com.simontuffs.onejar.BootOne-Jar-Main-Class: YOUR.MAIN.CLASS.NAME.HERE

5. In the last step, enter cd root in the command line to enter the root directory, and then run the java packaging command as follows:

Jar-cvfm ../your program name. jar boot-manifest.mf.

After running, if everything goes well, "your program name will be generated in the upper-level directory of the root directory. jar file. In windows, you only need to double-click the file to start your program. this is a very powerful file! Haha

Pay attention to the corresponding directory structure when packaging One-Jar. The detailed structure of the root directory is described as follows:

Root directory |. version | one-jar-$ project $. jar | OneJar. class | boot-manifest.mf # Your New boot-manifest.mf | com/simontuffs/onejar # One-Jar source file directory | Boot. class ,... etc. | doc/one-jar-license.txt # corresponding software protocol | main/main. jar # Your program JAR | lib/. jar... etc. # Third-Party JAR packages your program depends on

If everything is based on the above directory structure, there should be no problem. in fact, One-Jar can not only be used independently, but can also be used together with Ant and Meavn and other Java project source code management software. I will not elaborate on it here (haha, the key is that I have not tried it ~), Interested can go to http://one-jar.sourceforge.net/index.php? Page = getting-started & file = quickstart.

All roads are connected to Rome, not only One-Jar, but I also checked that UberJar or Shade has the same function. however, they still need to depend on Maven. corresponding to stackoverflow God's original words (location: http://stackoverflow.com/questions/183292/classpath-including-jar-within-a-jar ):

If you're trying to create a single jar that contains your application and it's required libraries, there are two ways (that I know of) to do that. the first is One-Jar, which uses a special classloader to allow the nesting of jars. the second is UberJar, (or Shade), which explodes the specified ded libraries and puts all the classes in the top-level jar. I shoshould also mention that UberJar and Shade are plugins for Maven1 and Maven2 respectively. as mentioned below, you can also use the assembly plugin (which in reality is much more powerful, but much harder to properly configure ).

About the Java initiator why cannot use Class-Path to load the JAR library in the JAR package, also found the corresponding explanation on stackoverflow (Source: http://stackoverflow.com/questions/12357136/reference-jars-inside-a-jar), as follows:

The Java Launcher $ AppClassLoader does not know how to load classes from a Jar inside a Jar with this kind of Class-Path. Trying to use jar: file: jarname. jar! /Commons-logging.jar also leads down a dead-end. this approach will only work if you install (I. e. scatter) the supporting Jar files into the directory where the jarname. jar file is installed.


The last small conjecture about the principle of One-Jar is that I want to use a tool class in the Java library to forcibly load the JAR package of the specified directory to the memory. then use the reflection mechanism to call the One-Jar-Main-Class item in the META-INF/MANIFEST file to specify the main method of the Class, so that the program can be started. but how to implement it requires a good study of the One-Jar source code ~


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.