The Java Archiver jar Tool


[Page 825 (continued)]

The Java Archiver jar Tool

The jar tool can be used to combine multiple files into a single JAR archive file. Although the jar tool is a general-purpose archiving and compression tool, it was designed mainly to facilitate the packaging of Java applets and applications into a single file.

The main justification for combining files into a single archive and compressing the archive is to improve download time. The jar command takes the following format:


jar [ options] destination-file input-file [input-files]

For an example of its usage, let's use it to archive the files involved in the WordGuess example in Chapter 9. As you may recall, this example used classes, such as TwoPlayerGame, and interfaces, such as IGame, that were developed in earlier sections of the chapter. To help manage the development of WordGuess, it would be useful to have a library containing the files that must be linked together when we compile WordGuess.

This is a perfect use of a jar file. Let's name our library nplayerlib.jar. We choose this name because the library can be used to create game programs that have N players, including two-player games.

For the two-player game, WordGuess, we want to combine all the .class files needed by WordGuess.java into a single jar file. Here is a list of the files we want to archive:

CLUIPlayableGame.class ComputerGame.class GUIPlayableGame.class IGame.class KeyboardReader.class Player.class TwoPlayerGame.class UserInterface.class 



[Page 826]

Assuming that all of these files are contained in a single directory (along with other files, perhaps), then the command we use from within that directory is as follows:

jar cf nplayerlib.jar *.class 


In this case, the cf options specify that we are creating a jar file named nplayerlib.jar that will consist of all the files having the .class suffix. To list the contents of this file, you can use the following command:

jar tf nplayerlib.jar 


Once we have created the jar file, we can copy it into the directory that contains our source code for the WordGuess program. We would then use the following commands to include the code contained in the library when we compile and run WordGuess.java:

javac -classpath nplayerlib.jar:. WordGuess.java java -classpath nplayerlib.jar:. WordGuess 


These commands, which use the -classpath option, tell javac and java to include code from the nplayerlib.jar. The notation :., links code in the current directory () with the library code.

Once we have created a library, we can also use it for Java applets. For example, suppose we have developed an applet version of the WordGuess game and linked all of the applet's code into a jar file named wordguessapplet.jar. The following HTML file would allow users to download the applet from their Web browser:

<html>      <head><title>WordGuess Applet</title></head>      <body>      <applet          archive="wordguessapplet.jar"          code="WordGuessApplet.class"          width=350 height=350      >          <parameter name="author" value="Java Java Java">          <parameter name="date" value="April 2005">      </applet>      </body> </html> 


When specified in this way, the browser will take care of downloading the archive file and extracting the individual files needed by the applet. Note that the code attribute must still designate the file where the program will start execution.




Java, Java, Java(c) Object-Orienting Problem Solving
Java, Java, Java, Object-Oriented Problem Solving (3rd Edition)
ISBN: 0131474340
EAN: 2147483647
Year: 2005
Pages: 275

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net