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

Page 2 of 3

Next, we extract the jar file's name. First, we ask for a URL to the class file containing the currently running class (which is the extractor program). Once we have the URL, we can snip out the jar file's name. By definition, the URL of the JAR extractor program follows the basic format:

  1. jar:, which shows that the executable runs from inside a jar file
  2. The jar file's URL, such as file:/C:/temp/test.jar, followed by the ! character
  3. The internal path name of the file within the JAR, such as /ZipSelfExtractor.class


In the extractor program's case, the URL might look like:

jar:file:/home/johnm/test/zipper.jar!/ZipSelfExtractor.class


Now that we have the jar file's name, we can perform the extraction. The guts of the extraction program rely on the built-in, Java zip/jar file manipulation libraries to uncompress the content files contained in the archive. See Resources for more on the zip/jar file manipulation libraries.

For ease of use, the extractor is a graphical Java application. The application uses the JFileChooser class to let users specify the destination directory to which they want the files extracted. A ProgressMonitor shows the progress of the extraction process. If a file might overwrite an already existing file, the user is asked whether or not to overwrite the existing file. At the conclusion, a standard dialog box presents extraction statistics.

Finally, the extractor program checks that it does not extract the files that make the jar file self-extracting -- the manifest file and the extractor's .class file; the program should just extract the original JAR contents. Those two files are artifacts of the self-extracting jar file and not part of the original, base content files.

Packing the jar file

Now that we have the manifest file and the extractor program, we can build the self-extracting jar file. We can manually use the JDK's jar utility to make a self-extracting jar file. For example, assuming you have a zip file called myzip.zip, you can perform the following steps to make a self-extracting file from it:

  1. cd to the directory containing myzip.zip
  2. Download zipper.jar
  3. Extract the files into the current directory. We've made it a self-extracting JAR:
    java -jar zipper.jar
    
  4. Copy the zipper.class file to ZipSelfExtractor.class
  5. Rename myzip.zip as myzip.jar
  6. Update myzip.jar with the jarmanifest and ZipSelfExtractor.class files:
    jar uvfm myzip.jar jarmanifest ZipSelfExtractor.class
    


Now myzip.jar is self-extracting on all platforms containing Java Runtime Environment (JRE) 1.2 or later. To execute the self-extracting jar file, run:

java -jar myzip.jar


Note that some platforms may have bindings already set up such that you can execute the jar file just by clicking on the myzip.jar file icon, which will run the command line equivalent.

Exercise for the reader

The current ZipSelfExtract does not integrate well if you make a self-extracting JAR out of an existing jar file containing a manifest file. Add intelligence to the self-extractor and the creation instructions so you can deal with existing jar files that contain manifest files.

  • Print
  • Feedback

Resources