How to make a self-extracting jar file

Source: Internet
Author: User
Tags tostring zip

This is an article that describes how to make a self-extracting jar file, the author through a self-extracting program, and the self-extracting program and a manifest file into the original jar file, to produce a variety of Java-enabled platform to run the self-extracting jar file.

Self-Extracting file

Let's take a look at the self-extracting file, where you can use a self-extracting tool such as WinZip Self-Extractor to make a self-extracting file, which packs a zip file with the decompression program to produce a new executable file. You can then unlock the contents of the zip file as long as you run the executable file. So why create a self-extracting jar file, create a self-extracting zip file is not good? We should note that the self-extracting jar file can be undone and executed on any Java-enabled platform, for example, can be performed under Linux. Creating a jar self-extracting file is simple, requiring only a special jar manifest file, a Java-based decompression program (which is written by the original author), a jar or zip file containing the base file, and any JSDK jar application

Manifest file

To generate an executable jar file, you need the MANIFEST file in the Meta-inf directory, file name: MANIFEST.MF, but here we only need to specify in the file that the Java-based decompression program contains main () The name of the class: Main-class:zipselfextractor

We have added a file called Jarmanifest to the source package for this technique.

Decompression Program

You can use a variety of methods to implement this decompression program, where we have a simple and straightforward approach. First, the decompression program to determine the name of the self-extracting jar file, with this file name, decompression program using decompression standard, the file to untie. The Zipselfextractor.java files in the source package can be viewed in detail.

It is worth mentioning that there is a clever way to get the file name of the jar file, although the name of the file that appears on the command line, but it does not pass as a parameter in the class's main (), the following code is used to get the file name:

private String getJarFileName ()
   {
    myClassName = this.getClass().getName() + ".class";
    URL urlJar =
      this.getClass().getClassLoader().getSystemResource(myClassName);
    String urlStr = urlJar.toString();
    int from = "jar:file:".length();
    int to = urlStr.indexOf("!/");
    return urlStr.substring(from, to);
   }

Note that myClassName is used instead of Zipselfextractor.class as parameters in Getsystemresource (), which allows us to change the name of the pressurized program without having to modify the code.

Next, we'll analyze the name of the jar file. First, you can get the file that contains the running class, urlstr = Urljar.tostring (), with this URL, remove the jar file name, the rest is what we want, and here's the format of the URL:

Jar:file:/home/test/zipper.jar!/zipselfextractor.class

With the filename, you can start decompression, detailed decompression algorithm please see the source code.

In order to be more convenient and practical, the program uses the graphical interface, the program uses the JFileChooser class to select the target directory to extract.

The final procedure also ensures that the two files are not: manifest files and extractor ' s. Class (Here is Zipselfextractor.class) The file is also solved, the program is used to unlock the original jar content, and these two files and belong to the jar original content.

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.