Chapter 14

Creating Your Own Class Archive Files

by Mark Wutka


Class Archive Files

Java classes are sometimes packaged together in special archive files. To date, the only officially allowed format for these files is the zip format.

When you install the JDK, or almost any Java-enabled browser, you will notice a .zip file somewhere in the files you have installed. This file is sometimes but not always called

Netscape, for instance, stores its files in or (The numbers indicate version numbers; they might also be 2_01, 3_01, and so on.)

These zip files hold Java .class files in uncompressed format. The Java runtime environment can't read a class file from a zip file if it has been compressed. Since compression is one of the major features of zip archiving programs, you must explicitly turn off compression when you create the archive.

Sun will soon add another archive format to the mix: the JAR format (Java ARchive). Instead of using the current method in which each class is downloaded individually, you should be able to package your classes in JAR files and download them in one large bundle. And you should not have to add any code to your programs to allow JAR packaging.

Creating Your Own Archive File with Info-ZIP

The first thing you need to create a class archive, other than the classes themselves, is a zip archiver. The Info-ZIP is an excellent archiver for creating zip files since it is free, available on a variety of platforms, and can store files in uncompressed format.

The main home page for Info-ZIP is located at and at a number of mirror sites. Info-ZIP is available in both source and binary form, with binaries available for Windows, OS/2, VMS, most major UNIX platforms, and more. Just follow the instructions on the Info-ZIP Web page for downloading and installing for your platform. Figure 14.1 shows the Info-ZIP Web page.

Figure 14.1 : The Info-Zip program is available for a wide variety of systems.

If the classes in your archive belong to a package, the path name of each class must have the package name. For each subpackage name, you must also have a directory.

For instance, if you have a class that belongs to a package called: mylib.awtstuff. interfaces, you must have a directory called mylib holding a subdirectory called awtstuff, which itself must hold a directory called interfaces. Your class must then be stored in the interfaces directory.

Once you've unpacked the zip program, don't forget to add it to your command path.

To archive the entire mylib package into a file called, go to the parent directory of mylib and type:

zip -r -0 mylib

The -r argument tells zip to include subdirectories in the archive. Without this argument, you would have to include the full path name of every file in the mylib directory.

The -0 option is extremely important because it stores the files with no compression. If you don't use the -0 option, you still create a zip file but Java can't use it, because zip compresses files by default.

When you create a zip archive, remember to add the full path name of the zip file to your CLASSPATH variable. Although class files are picked up when their directory name is in the CLASSPATH, .zip files must be mentioned explicitly. For example, if you created a file called, your CLASSPATH under Windows might look like: CLASSPATH=C:\mystuff\;C:\JAVA\LIB\CLASSES.ZIP.

Viewing the Contents of a Zip Archive

Once you have become familiar with the known Java classes, you might want to start exploring the hidden classes in the various browsers and Java environments. Begin by finding out what files are stored in the browser's class library. Use the unzip program from Info-ZIP.

The following command displays the contents of a class archive named

unzip -v

These class archives typically hold hundreds of .class files, so be prepared for many pages of output. Suppose you are browsing through the file from the JDK and notice a class file called sun/net/www/protocol/http/HttpURLConnection.class. You can get a copy of this class with the command:

unzip sun/net/www/protocol/http/HttpURLConnection.class

This extracts the file and creates a nice chain of subdirectories off your current directory. From your current directory, assuming that "." is in your CLASSPATH variable, you can examine the method and variable names in this class with the javap command:


Notice the relationship between package names and subdirectories. For each "." in the class name, there is a "/" (under UNIX) or a "\" (under Windows and OS/2) in the .class file path.

If you are really daring, you can use javap -c to disassemble the code in the .class file:

javap -c

Before disassembling any code that comes with a license agreement, look over the license agreement closely. Many vendors explicitly forbid the disassembling of their code. Although they are probably more worried about you stealing their trade secrets than just seeing what their code does, you are still breaking the law if you disassemble their code without permission.

Adding Classes Directly to the Browser's Library

Your browser loads classes from the local filesystem if they are included in its CLASSPATH variable. Sometimes, however, you may want to add classes directly to the browser's zip library.

Changing a browser's own class archive is dangerous. You could accidentally introduce a class that bypasses the applet security restrictions.
You could also accidentally overwrite a necessary class and cause your browser to stop working. Always make a backup copy of the original zip file before making any changes, and make sure you know what you are doing.

Adding classes to a zip file is no different from creating a new zip file. To add the mylib package to a class archive named, use the following command:

zip -0 mylib

You can also replace classes in a class archive. For instance, you may want to insert a dummy security manager that removes any restrictions on applets and applications.

The security manager for each browser is in a different package, so you need to adjust your dummy security manager and the zip command for each browser. To insert a dummy security manager into Netscape, for instance, you need to replace netscape.applet.AppletSecurity.

The following command does that for Netscape version 2:

zip -0 netscape/applet/AppletSecurity.class

You need to have copied AppletSecurity.class into the netscape/applet (netscape\applet under Windows) subdirectory. Again, this is a dangerous thing to play with. You could open up your entire company's network to malicious hacking just by replacing classes in your own browser.

Creating Class Archives with Other Zip Archivers

You can use any archiver to create zip archives for use with Java as long as you can turn off the file compression. For example, the PKZIP 2.04 command-line option to turn off compression is -e0. Unfortunately, PKZIP does not support long filenames, so its use in archiving Java classes is limited.

The WinZip utility provides a nice graphical interface for creating zip files. You can find WinZip on the Web at WinZip is a shareware program, so you may try it out for a period of time before deciding to buy it. You will probably discover that it is well worth the price.

One of the advantages of WinZip is that you may view a zip archive in a window, and view any single file stored in the archive, without having to unpack it first. Since WinZip recognizes the Windows file types, when you double-click a file, WinZip brings up the appropriate viewer for that file.

When you create a zip archive with WinZip, you can switch off compression from the screen where you enter file names. Figure 14.2 shows the WinZip screen on which you can switch off compression.

Figure 14.2 : Select "None" for compression type in WinZip.

Creating Cabinet Files for Internet Explorer

Microsoft Internet Explorer supports an alternative to zip files called cabinets. Cabinets can include .class files, images, and audio clips. Cabinets are not limited to Java files, however. IE supports many other kinds of executable content, and any of them may be packaged in a cabinet file. Cabinets may also contain compressed data, allowing you to download code quickly.

The data compression, along with the fact that you can group many different file types in a cabinet, make cabinets an attractive alternative to zip files. The biggest disadvantage to cabinets is that they are currently available only under Internet Explorer.

In order to store files in a cabinet, you need the Cabinet Development Kit (CDK) from Microsoft. This is available from Microsoft's Web site at

Once you have downloaded and unpacked the CDK, you can use the cabarc program to create cabinets. Make sure that your PATH setting includes the directory where you unpacked the CDK. The command-line for the cabarc program is very similar to that of a zip archiver:

cabarc n cabfilename files

For example, to pack all the .class files and all the .gif files in the current directory into a cabinet file called, you would use the following command:

cabarc n *.class *.gif

If you want to include a whole directory tree, add the -r and -p options to cabarc. For instance, to package the current directory, including all subdirectories, and store them in a file called, you would use the following command:

cabarc -r -p n *.*

Once you have created a cabinet file, you can use it in a Web page by including the following applet parameter:

<PARAM name="cabbase" value="cabfilename">

The following example .html file loads an applet called MyApplet from a cabinet file called

<TITLE>Cabinet Example</TITLE>
<APPLET codebase="." code="MyApplet.class" width=250 height=250>
<PARAM name="cabbase" value="">
You need a Java enabled browser to see this program.

You don't have to do anything special to access images and audio clips that are stored in cabinet files. Just access them via the codebase or document base URLs as you normally would. The IE browser will take care of the rest.

For Java 1.1, Sun has defined an archive format called JAR (Java ARchive) that is very similar to the cabinet format. It also supports data compression and can store multiple types of files. Since the JAR format will be part of future releases of Java, it will be available on any Java-compliant platform.