|
|
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
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.
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.
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.