Java Programming   Java Programming
 |Sofia Home | Content Gallery |
Home
Syllabus
Schedule
Lessons
Assignments
Discussion
Resources

Lesson 11.6 JARs

Using Java Archives

One of the difficulties with distributing a Java program, is the sheer number of files that have to be managed. Even though you don't have to download the built-in Java classes like Button and Label, its not unheard of to have dozens, or even hundreds of separate files for a very modest program.

When you add images, things get even worse. Here, for instance, is a simple skeleton for a card playing program. The Cards applet uses only a single, small class, but requires 52 different images for the each of the cards as well as a couple of audio-clip files. Even though the total download is way under 50KB, the time required to make all of those connections to the Web server can really make the download appear slow.

Back to Top

What are JARs?

Java Archive files [or JAR files] are combined and compressed files, [using ZIP compression], that can store all the classes used by your applet in a single file. When your applet is downloaded, your browser, rather than making separate connections to your Web server for each piece of the applet, retrieves the pieces out of the JAR file.

Wonderful. Or, it would be, if it always worked. Despite the promise of JAR files, the reality is not as bright as it might seem at first glance. You might be interested in reading this JavaWorld article on JAR files which goes into more depth on the pros and cons.

Essentially, here are the problems with JAR files:

  • Don't work at all in Java 1.0 browsers. This includes earlier versions of AOL's browser as well as IE 3 and NS 3. If you use JAR files, a lot of folks won't see your applet!
  • Each of the Java 1.1 enabled browsers supports loading class files from the JAR file. If your application is made up of a lot of separate classes, and only a few other resources, then you won't have any problem using JARs.
  • Each of the Java 1.1 enabled browsers supports a different way of retrieving images or other resources [such as AudioClips] from a JAR file. That means you have to keep different versions of your applet source code for each browser you support. This is not good. Only IE 4 [and maybe 5] treat the resources in your JAR file just as if they were loaded from a URL.
Given those drawbacks, you might wonder why you'd want to use JARs at all. For Internet applet, you probably don't want to, unless you can ensure that all your viewers are using IE, or if you unbundle your graphics and audio resources.

JAR files are, however, great for bundling applications or applets that users download and run on their local machine. And, since our next lesson covers Java applications, this is a good time to learn to use the JAR tool.

Back to Top

Making a JAR

Let's look at the steps necessary to package the Cards applet, shown above, in a JAR file. Here are the pieces we have to work with:

The Cards application consists of a single class file, Cards.class, which you can see is 4,769 bytes long.

DOS listing showing the Cards.class file
The images and sounds used by the Cards applet are contained in two subdirectories, located "below" the directory where Cards.class is located. The two subdirectories are cards/ and audio/ as shown here:
DOS listing showing the cards and audio subdirectories
The cards/ subdirectory contains 53 separate GIF files, for a total of 14,036 bytes, as you can see here:
DOS listing showing the total number of files [and bytes] in the cards subdirectory
The audio/ subdirectory contains two separate AU files for a total size of 16,018 bytes as you can see.
DOS listing showing the files in the audio subdirectory.
The total size of all of the pieces is 34, 823 bytes, which is quite small.

The jar Command

To combine all of these pieces into a single file, we use the jartool. This is a command-line program, like the javac compiler, and located in the same directory. If you start a regular JDK session, and then type jar, you'll see the "help" screen shown below:
Help text displayed when using the jar tool with no arguments.

To create a new file, you simply type jar cf jar_file_name like this:

jar cf cards.jar

The cf command means create file. The problem, of course, is that our jar file is empty. In addition to creating the JAR, we need to put some content into it.

When you specify the contents of the JAR file, you can either:

  • Name each file individually.
  • Use wildcards to specify a particular type of file.
  • Use a directory name, in which case the entire directory and all subdirectories are included in the JAR file.

In our case, we need to name one file explicitly [Cards.class], and two subdirectories [audio and cards]. Our jar tool command line should look like this:

jar cf cards.jar Cards.class audio cards

As you can see, this creates a JAR file named cards.jar. Of course, the compression is nothing to write home about. The file is larger than our individual files.

DOS listing showing the cards.jar Java Archive file

Back to Top

Using a JAR File

Once you have a JAR file, you use an additional <APPLET> tag attribute named ARCHIVE to tell appletviewer or your Web browser to look inside the JAR file to find its classes.

Here's the <APPLET> tag to view the Cards applet:

<APPLET CODE=Cards.class 
        ARCHIVE=cards.jar 
        WIDTH=400 
        HEIGHT=200>
</APPLET>

Note, even when you use the ARCHIVE attribute, you still have to supply the CODE attribute as well. The CODE attribute tells your browser, or appletviewer, what class to load. The ARCHIVE attribute tells it where to find the code.

Feel free to download and run the cards.jar file using appletviewer. To download the JAR file using Navigator, make sure you right-click the hyperlink and save it on your computer. If you simply do a regular-click, Navigator will try to run "SmartUpdate" and tell that you have a "corrupt" JAR file. Of course, since it's only an unfinished card game, the message is in error. If it were video poker, on the other hand, they'd have a point.

You can also view the source code for Cards.java.

Something to Talk About

How can you use the jar tool to combine both of the files from Homework 9 [Spinner.class and TestSpinner.class] into a single JAR file?
  • Whichjar tool command line is needed?
  • Which <APPLET> tag is needed to run your applet in appletviewer?

This is the last section of this lesson.

 

Back to Top

 

Content Developed by Stephen Gilbert, Licensed under a Creative Commons License
Published by the Sofia Open Content Initiative
© 2004 Foothill-De Anza Community College District &The William and Flora Hewlett Foundation