Sams Teach Yourself Java 2 in 24 Hours

Sams Teach Yourself Java 2 in 24 Hours

By Rogers Cadenhead

Java Archives

Applets that use sound or graphics often require several different files to be loaded before the program can run successfully. The workshop you will undertake during this hour includes 12 sound files.

Because of the way the World Wide Web functions, each of these files requires its own connection between the Web server offering the applet and the user running the program during a Web surfing session. The same is true of World Wide Web pages—each graphic requires its own connection.

One way to reduce the time required to load an applet is to package all its files into a single archive file. The Software Development Kit includes a command-line tool called jar that enables Java archive files to be created, examined, and unpackaged.

Java archive files have the .JAR file extension. The jar tool is used to package a group of files into a single .JAR file. If you have created a SoundFX applet that includes 10 WAV files that have names beginning with fx, the following command could be entered at a command-line to package the applet and WAV files into a single JAR archive:

jar cf SoundFX.jar SoundFX.class fx*.wav 

No file folders are included with the filenames in this example, so it must be used in the same folder that contains SoundFX.class and the WAV files used by the applet.

The jar tool takes the following arguments when it is run:

Wildcards can be used when specifying filenames, so fx*.wav refers to any file with a name that begins with fx and ends with .wav. You can also list each file separately.

After you have created a Java archive containing all files used in an applet, the ARCHIVE attribute of the <APPLET> tag is used to associate this archive with the applet.

Continuing the example of the SoundFX applet, you could include its JAR archive on a Web page with the following HTML code:

<applet code="SoundFX.class" archive="SoundFX.jar" 
height=80 width=140> 
</applet> 

When the ARCHIVE attribute is used with an applet, the applet's main class file should be included in the specified archive file. The CODE attribute must still be used because the name of the main class file must still be identified, whether it's loaded directly or from an archive.

Java archives are a way to speed up the presentation of applets and to organize other multifile projects. All JavaBeans components are packaged into .JAR files, making them more self-contained and keeping them in a single file that's easier to make available for use in other Java programs.

Share ThisShare This

Informit Network