Jar files

Most significant Applets will consist of more than one Class file. The problem with loading multiple class file through a web browser is that the HTTP protocol means the browser has to "negotiate" the process of starting and closing a connection for every individual file. This can add a significant overhead if there are multiple files. Thus it can take a disproportional time to load an applet, by contrast with how long it would take to load a single class file amounting to the same file size. There is a solution to this problem in that Java class files can be wrapped up in a single Jar file that contains the multiple class files required for the entire applet. The following is a quote from the documentation for the jar utility.

When the components of an applet or application (.class files, images and sounds) are combined into a single archive, they can be downloaded by a Java agent (like a browser) in a single HTTP transaction, rather than require a new connection for each piece. This dramatically improves download time. The jar tool also compresses files, which further improves download time.

The command line syntax of the jar program can seem somewhat arcane, however if you have a background in Unix/Linux the syntax is similar to that of the tar archiving program. If you are a mere mortal you might like to become familiar with the two following command lines.

Packing a Jar


jar -cvf  MyJar.jar MyClass.class



Unpacking a Jar



jar -xvf jarfile.jar



If it helps as a memory jerker you can consider the command lines to mean

-create -verbose -file

and

-xtract -verbose -file

of course if you are a mere mortal it may seem entirely spurious to have to specify that you want to create to a file or extract from a file, but this syntax was "inspired" by UNIX where there is no such thing as too much detail