Click to order Toshiba's Equium desktop computer

PC Magazine

  PC Tech

Java Archives in Java 1.2

Introduction

Working With Java Archive Files

Using Archived Applications and Applets

Bye-Bye, <applet>, Hello, <object>

Figure: Java Archive Commands



X10.com - The SuperSite for Home Automation!

NextCard Internet Visa - Apply Now

 
  Categories
Programming

Java Archives in Java 1.2
Working With Java Archive Files

Continued from Introduction

An applet is a Java program designed to be run within a browser. If you've ever worked with applets, you know that a single Java program can have dozens of source files associated with it. There are files for the Java classes that store the actual code, files for graphics used in the applet, and files for other types of media as well. To make it easier for programmers and nonprogrammers alike to use Java applets, Java 1.1 introduced an archive system that allows you to bundle all the source files for an applet into a single file called a Java archive (JAR).

Here, the term archive refers simply to a file that contains a collection of files. If you've ever backed up your hard disk to tape or used a ZIP compression tool, you've already worked with archives. Just as most ZIP tools compress files before storing them in the archive to save space, files in Java archives are also compressed to save space. In fact, Java uses the ZIP compression format in its archives.

An added bonus of using compressed archive files is that they can greatly improve the performance of applets, and these performance benefits extend to both sides of the client/server Web equation. Compressed archives take up less file space, and your browser can download these archives many times faster than the original uncompressed files. On the Web server, the time savings are also substantial. With separate files, the server generates a separate HTTP transaction for each file. Each transaction uses server resources and can take a few seconds to generate. Thus, by bundling many files into a single file, you can speed up the server's response time and reduce the dreaded wait--and we all hate to wait.

Until recently, Java archives were used primarily with applets, and because of this most people think of a Java archive as something you can use only with applets. However, Java 1.2 introduces enhancements so that you can easily create and use archives for applications, too. If the Java Development Kit is installed on your computer, you can create archives with the Java archive tool, JAR. Normally you run JAR from the DOS or Unix command line. Figure 1 summarizes the syntax for the most common uses of JAR.

Unlike most other command line tools, JAR doesn't require you to use a hyphen or slash to specify options. When you create an archive, you normally pass JAR two options: c to create the archive and f to specify the filename of the archive. These options are followed by the archive name, which should end with the .jar extension, and the list of files to include in the archive. In this example, JAR creates an archive called myarchive.jar that contains the files ccMain.class, vid.class, and banner.gif:

jar cf myarchive.jar ccMain.class vid.class banner.gif

If you've obtained an archive created by someone else, you may want to know what files the archive contains. To do this, you can use the t option to show the archive's table of contents. The following example examines the table of contents for an archive called myarchive.jar:

jar tf myarchive.jar

Usually, you will want Java to tell you exactly what's happening when you are archiving files or trying to display the table of contents; the v (for verbose) option lets you do this. You combine this option with the other options discussed previously. When you create archives with the c, v, and f options, JAR gives you a great deal of information about the archive process. You'll see the files being added to the archive, the original sizes of those files, the compressed file sizes, and how much space you've squeezed out of the files. For example, if you create the following archive:

jar cvf keyarchive.jar keys.class comp.class talk.class

the Java archive tool would detail the main archival process something like this:

adding: keys.class in=19612 out=4810 deflated 75%
adding: comp.class in=9554 out=4105 deflated 57%
adding: talk.class in=42453 out=9021 deflated 78%

Here the Java archive tool adds three class files to the archive. The original file sizes are shown with the in parameter. The compressed file sizes are shown with the out parameter. And the amount of space squeezed out of each file is shown last. As you can see, the original size of the three files is about 70K, yet JAR manages to squeeze the files into an archive of about 18K--quite a difference when you are transferring files using a standard modem.

Next: Using Archived Applications and Applets

Published as Internet Builder in the 2/23/99 issue of PC Magazine.

 
 SPONSORED LINKS
@Backup   Your Solid Online Backup Plan. Download Now.
Services   9c/MINUTE LONG DISTANCE, 5c/MINUTE ON SUNDAYS!
STORAGE   Quantum means non-stop business, 24 hours a day
Software   X10.com -- The SuperSite for Home Automation
Books   Bargain Books up to 90% off at barnesandnoble.com
 ZDNET FEATURED LINKS
Downloads   Check out the best new downloads in Reviewer's Raves
Bargains!   Shop the Basement for best buys on computer products
Free Help   Got computing questions? ZDHelp has all the answers!
 MAGAZINE OFFERS
Free Offer   Get a FREE SUBSCRIPTION to Inter@ctive Week

TOP
Copyright (c) 1998 Ziff-Davis Inc.