Java Tip 21: Use archive files to speed up applet loading

Crank up the perceived performance of your applet by making it load faster

One of the ongoing complaints about Java is its performance. A big factor in the user's perception of the speed and value of Java applets is the time it takes to download all of the classes that make up the applet. We have all had the pleasure of waiting a minute or more for an applet to download just to see some silly animated graphic. Unfortunately, this negative performance perception can spill over to applets that are actually useful.

To understand why the new applet loading method is fast, you need to know why the current method is so slow. A given applet usually is composed of a number of Java .class files. For each of those class files, the class loader must open an individual socket connection from your browser to the server that the applet code resides on. So, if your applet is composed of 18 .class files, the browser must open at least 18 sockets so it can transfer each one of those files. The overhead to set up and tear down each of those connections is quite significant. For example, each connection set up requires a number of network packet round-trips, which greatly increases the overall latency (especially in these times of the increasingly congested 'Net). (For the nitty-gritty details of socket connection overhead, check out one of the weighty TCP/IP reference tomes.)

By this point you have already figured out the solution to this problem: Put all of the .class files into one big file so only one connection must be made to download everything. Good thinking! That's exactly what the folks at the two big Java browser camps (Netscape and Microsoft) thought of.

Unfortunately, the two solutions that they came up with are not directly compatible. Microsoft, in its need to be different, created its own CAB file format. The Netscape solution is to use the existing, well-known .zip archive file format. Luckily, we can write our HTML code to handle both formats if we want to. This is because each of those special file formats is specified by a separate extension to the &ltAPPLET> HTML tag.

I'm not going to speak to the creation of CAB files (since they should be going away). For those who are really interested, check out the Microsoft Java developer documentation. Once you have created a CAB archive you can use it by adding a cabbase HTML parameter to the <APPLET> tag:

&ltapplet name="Hello" code="HelloWorld" width="50" height="50">
   &ltparam name=codebase value="http://www.foo.com/classes">
   &ltparam name=cabbase value="hello.cab">
</applet>

The value of the cabbase parameter is the name of the CAB file.

Creating a .zip file archive that can be used with the Netscape browser is easy. Zip up all of the .class files that are needed for your applet into a single .zip file. The only thing to remember is that you must only store the files into the archive (that is, no compression)!

If you are using PKZip:

pkzip -e0 fileArchive.zip listOfClassFiles

If you are using the Info-Zip Zip program:

zip -0 fileArchive.zip listOfClassFiles

Note that in both cases, the command-line flag contains a zero rather than a letter "O";

Use the .zip archive in your HTML file by specifying the archive tag in the applet section:

&ltapplet name="Hello" code="HelloWorld" width="50" height="50"
   codebase="http://www.foo.com/classes"
   archive="hello.zip">
</applet>

But, wait -- there's more! You can create .cab and zip archives and make them both available; this means Navigator and Internet Explorer users will get faster applet downloads. There's no need to worry about users of older versions of either of these browsers -- or of any other browser -- since they will still get your applet's classes via the older, slower method. Putting all of the pieces together:

&ltapplet name="Hello" code="HelloWorld" width="50" height="50"
   codebase="http://www.foo.com/classes"
   archive="hello.zip">
   &ltparam name=codebase value="http://www.foo.com/classes">
   &ltparam name=cabbase value="hello.cab">
</applet>

Now that you can do this with .cab and zip file archives, I guess I should tell you that as part of the Java JDK v. 1.1, JavaSoft has defined a new archive file format in which you can put all of your images, audio, and class files (see the Resources section). JavaSoft calls this the Java Archive (JAR) format. The <APPLET> HTML tag is modified to handle this JAR format with the archives parameter. I leave it up to you to put all three archive file formats together.

Learn more about this topic