«^»
4.11. Using the Java archive tool

As was mentioned earlier, the CODE attribute in the HTML that is used to run an applet identifies the name of the file containing the bytecodes of the applet's class. However, normally, the author of an applet will provide a number of supporting classes as well as the class of the applet. It was pointed out earlier that the bytecodes of each of the .class files will be downloaded from the author's WWW site as the Java interpreter being used by the WWW browser detects that it requires them.

For example, suppose a directory contains the files for the GetDateApplet applet. The following files would have to be downloaded in order to execute this applet: GetDateApplet.class and JButtonListener.class. Obviously, programs are usually a lot more complicated than this: such programs may have a large number of .class files.

The Java 2 SDK (or the JDK) contains a tool that enables the author of an applet to combine a number of files into a single file. The resulting file is called a Java Archive. The tool is called jar, and, like the other commands of the SDK/JDK, it can be run from a Unix/MS-DOS command line. The documentation for the jar command says When the components of an applet or application (.class files, images and sounds) are combined into a single archive, they may be downloaded by a Java agent (like a browser) in a single HTTP transaction, rather than requiring a new connection for each piece. This dramatically improves download times. jar also compresses files and so further improves download time.

Assuming that the directory containing the files for the GetDateApplet applet only contains .class files that are associated with this applet, then a Java Archive can be produced from these .class files by the Unix/MS-DOS command line:

jar cvf GetDateApplet.jar *.class
The first argument to the jar command, which in this example is cvf, indicates the options that you want to be passed to the jar command. There are three main ways in which the jar command is used. If the options contain a c, then this means that you want to create an archive; if they contain a t, you want the jar command just to list the contents of an archive (that already exists); and if they contain an x, you want the command to extract some .class files from an archive.

If the options include the letter v, then the jar command will produce some output to tell you what it is doing - means verbose. Finally, the f means that the name of an archive is given as the next argument. When a c option is present, the remaining arguments give the names of the .class files that you want to be put into the archive. In Unix/MS-DOS, the notation *.class refers to all of the files in the current directory that have a .class extension.

So the above jar command produces a file called GetDateApplet.jar that contains a compressed archive of the two .class files that constitute the GetDateApplet applet.

Suppose a WWW page contains a CODE attribute to say that the bytecodes of an applet's class are stored in the file GetDateApplet.class. If you want the WWW browser to download the bytecodes in the Java Archive GetDateApplet.jar instead of downloading each .class file, you will need to include an ARCHIVE attribute as well as the CODE attribute.

If your HTML uses an APPLET tag or an EMBED tag, the syntax of the ARCHIVE attribute is:

archive="GetDateApplet.jar"
and, if your HTML uses an OBJECT tag, you need to include:
<PARAM NAME="archive" VALUE="GetDateApplet.jar">
Note you need a CODE attribute as well as the ARCHIVE attribute: the latter gives the name of the file containing the Java Archive (in which all the .class files are stored) and the CODE attribute gives the name of the class file that contains the class of the applet, i.e., effectively it identifies the bytecodes that are executed first.

As mentioned earlier, there are two advantages in using a Java Archive:

Here is a link to a WWW page containing HTML instructions that use the file GetDateApplet.jar: http://www.dur.ac.uk/barry.cornelius/papers/advanced+/code2/Applets/GetDateJar/GetDateApplet.html.