Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Java Tip 120: Execute self-extracting JARs

Create easy-to-use, cross-platform Java Archives

  • Print
  • Feedback
For many years, Phil Katz's archive creation, zip, has been one of the most popular file archiving formats. Sun has adopted the zip format as the basis for the Java Archive (JAR). Sun has extended the zip format's use with various conventions so that you can pack Java classes into an archive file. With the addition of the JAR manifest file, the Java runtime can easily locate and directly execute the main class of the Java application contained in the jar file.

Some zip utility tools can create self-extracting archives for various platforms, such as MS Windows. The utility tool combines a regular zip archive with an extractor program to generate a new executable (exe) file. Receivers of the exe file only need to run it to extract the original zip archive's contents. The executable runs the extractor program to extract the archived files into a user-specified output directory.

You can convert a base zip or jar file into an executable jar file on any Java platform. Whereas the self-extracting zip can only create platform-specific executables, the self-extracting jar file can be distributed to and run on any platform supporting Java.

Creating the self-extracting jar file is straightforward. You just need a special JAR manifest file, a Java-based extraction program, the zip or jar file containing the base content files, and any Java SDK's jar utility application.

The manifest file

To make executable JARs, you first need a manifest file called MANIFEST.MF in the META-INF directory. The manifest file may contain a number of possible entries; however, for our purposes here, we just need to specify the name of the Java class that contains the Java-based extractor program's main() method:

Main-Class: ZipSelfExtractor


We've added a manifest file named jarmanifest to this tip's example code. For more information about the manifest file, see the Jar File Specification.

The extractor

You can make the extractor program using various approaches. The approach we present here is simple and straightforward. First, the extraction program figures out the self-extracting jar file's name. With that name in hand, the extractor utilizes the standard, built-in Java zip/jar libraries to extract the content files from the archive. You can find the full source code for ZipSelfExtractor in ZipSelfExtractor.java.

Getting the jar filename in the extractor program can be tricky. Although the jar file's name appears on the command line, that name is not passed to the class's main() method. Therefore, in the extractor program, we use the following code to extract the information from the URL that points to the extractor:

    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);
    }


Notice that in the getSystemResource() method we pass myClassName instead of ZipSelfExtractor.class. That lets us change the extractor program name without changing that part of the code. We set myClassName by looking up the current class's name.

  • Print
  • Feedback

Resources