jar Java File Archival Tool

When you deploy a Java program, keeping track of all the class files and other files required by the program can be cumbersome. To make this easier, the kit includes a tool called jar that can pack all a program's files into a Java archive—also called a JAR file. The jar tool also can be used to unpack the files in one of these archives. JAR files can be compressed using the Zip format or packed without using compression. To use the tool, type the command jar followed by command-line options and a series of filenames, folder names, or wildcards. The following command packs all a folder's class and GIF image files into a single Java archive called Animate.jar:

jar cf Animate.jar *.class *.gif

The argument cf specifies two command-line options that can be used when running the jar program. The c option indicates that a Java archive file should be created, and f indicates that the name of the archive file will follow as one of the next arguments. You also can add specific files to a Java archive with a command such as the following:

jar cf MusicLoop.jar MusicLoop.class muskratLove.mp3 shopAround.mp3

This creates a MusicLoop.jar archive containing three files: MusicLoop.class, muskratLove.mp3, and shopAround.mp3. Run jar without any arguments to see a list of options that can be used with the tool. One use for jar is to put all files necessary to run a Java applet in a single JAR file. This makes it much easier to deploy the applet on the Web. The standard way of placing a Java applet on a web page is to use APPLET or OBJECT to indicate the primary class file of the applet. A Java-enabled browser then downloads and runs the applet. Any other classes and any other files needed by the applet are downloaded from the web server. The problem with running applets in this way is that every single file an applet requires—helper classes, images, audio files, text files, or anything else—requires a separate connection from a web browser to the server containing the file. This can significantly increase the amount of time it takes to download an applet and everything it needs to run. If you can reduce the number of files the browser has to load from the server by putting many files into one Java archive, your applet can be downloaded and run by a web browser more quickly. If the files in a Java archive are compressed, it loads even more quickly. After you create a Java archive, the ARCHIVE attribute is used with the APPLET tag to show where the archive can be found. You can use Java archives with an applet with tags such as the following:

<applet code="MusicLoop.class" archive="MusicLoop.jar" >

This tag specifies that an archive called MusicLoop.jar contains files used by the applet. Browsers and browsing tools that support JAR files will look inside the archive for files that are needed as the applet runs.

Watch Out!

Although a Java archive can contain class files, the ARCHIVE attribute does not remove the need for the CODE attribute. A browser still needs to know the name of the applet's main class file to load it.

When using an OBJECT tag to display an applet that uses a JAR file, the applet's archive file is specified as a parameter using the PARAM tag. The tag should have the name attribute "archive" and a value attribute with the name of the archive file. The following example is a rewrite of the preceding example to use OBJECT instead of APPLET:

<object code="MusicLoop.class" >
 <param value="MusicLoop.jar">