Core Java™ 2 Volume I - Fundamentals, Seventh Edition

 The calculator applet from this chapter uses four classes: CalculatorApplet,CalculatorPanel, and two inner classes. You know that the applet tag references the class file that contains the applet class:
....

When the browser reads this line, it makes a connection to the web server and fetches the file CalculatorApplet.class. The class loader of the browser's virtual machine loads the CalculatorApplet class from that file. During the loading process, the class loader must resolve the other classes used in this class. After doing so, it then knows it needs more classes to run the applet. The browser, therefore, makes additional connections to the web server, one for each class file. Loading such an applet over a slow network connection can take many minutes.
NOTE


It is important to remember that the reason for this long loading time is not the size of the class files—they are quite small. Rather it is because of the considerable overhead involved in establishing a connection to the web server.

Java supports an improved method for loading class files, allowing you to package all the needed class files into a single file. This file can then be downloaded with a single HTTP request to the server. Files that archive Java class files are called Java Archive (JAR) files. JAR files can contain both class files and other file types such as image and sound files. JAR files are compressed, using the familiar ZIP compression format, which further reduces the download time.
You use the jar tool to make JAR files. (In the default installation, it's in the jdk/bindirectory.) The most common command to make a new JAR file uses the following syntax:

jar cvf JARFileName File1 File2 . . .
For example,
jar cvf CalculatorClasses.jar *.class icon.gif

In general, the jar command has the format

jar options File1 File2 . . .
Table 10-3 lists all the options for the jar program. They are similar to the options of the UNIX tar command.

Table 10-3. jar Program Options
Option
Description
c
Creates a new or empty archive and adds files to it. If any of the specified file names are directories, then the jar program processes them recursively.
t
Displays the table of contents.
u
Updates an existing JAR file.
x
Extracts files. If you supply one or more file names, only those files are extracted. Otherwise, all files are extracted.
f
Specifies the JAR file name as the second command-line argument. If this parameter is missing, then jar will write the result to standard output (when creating a JAR file) or read it from standard input (when extracting or tabulating a JAR file).
v
Generates verbose output.
m
Adds a manifest to the JAR file. A manifest is a description of the archive contents and origin. Every archive has a default manifest, but you can supply your own if you want to authenticate the contents of the archive.
0
Stores without ZIP compression.
M
Does not create a manifest file for the entries.
i
Creates an index file (see below for more information).
C
Temporarily changes the directory. For example,
jar cvf JARFileName.jar -C classes *.class

changes to the classes subdirectory to add class files.


Once you have a JAR file, you need to reference it in the applet tag, as in the following example.
.........

Note that the code attribute must still be present. The code attribute tells the browser the name of the applet. The archive is merely a source where the applet class and other files may be located. Whenever a class, image, or sound file is needed, the browser searches the JAR files in the archive list first. Only if the file is not contained in the archive will it be fetched from the web server.
TIP


If you have a large applet, chances are that not all users require all of its functionality. To reduce the download time, you can break up the applet code into multiple JAR files and add an index to the main JAR file. The class loader then knows which JAR files contain a particular package or resource. Seehttp://java.sun.com/j2se/5.0/docs/guide/jar/jar.html#JAR%20Index for details about the indexing procedure.

TIP


JDK 5.0 supports a new compression scheme, called "pack200", that is specifically tuned to compress class files more efficiently than the generic ZIP compression algorithm. Sun claims a compression rate of close to 90% with multi-megabyte JAR files that contain only class files. In order to deploy these files, you need to configure the web server so that it serves standard files for traditional clients and compressed files for new clients that indicate in the HTTP request that they are able to decompress them. Seehttp://java.sun.com/j2se/5.0/docs/guide/deployment/deployment-guide/pack200.html for detailed instructions.

NOTE


The Java Plug-in will cache applet code. Seehttp://java.sun.com/j2se/5.0/docs/guide/plugin/developer_guide/applet_caching.htmlfor details on tweaking the cache