Creating and Viewing a JAR

   

Creating and Viewing a JAR

The JARJAR J tool allows you to create, list, and extract files from JAR archives. It deliberately resembles the Unix tar tool, both in function and in usage. Like other tools in the SDK, the JAR tool is implemented as a Java application, making it portable to any platform supporting Java.

The jar command is used from the command line exclusively. There are many options that you provide to the jar command, depending on what you are trying to do. You can type just jar on the command line to see the options. For a detailed look at the options, see Appendix B, "SDK Tools."

To create a new JAR file, you can use the c option with the jar command. Options can be appended together. When creating a new JAR file, most often you want use cvf. The c option tells the jar command to create a new archive. The v option tells jar command to output verbose diagnostic messages to the console while it is working so you can see what is being added. The f option tells jar command to create an archive file of the given name . Take a look at this example:

 jar cvf foo.jar *.class images 

The name of the new JAR file must come after the f option. This command, when run from the command line, creates a new JAR archive named foo.jar in the current directory. The archive will contain all the class files in the current directory, as well as the complete images directory and all its contents, assuming those directories existed.

As an example, open up a console window and change to the JAVA_HOME/demo/applets/ TicTacToe directory. This directory should be under your JDK 1.3 home installation directory. Listing 25.1 shows the directory contents of the directory.

Listing 25.1 Listing of the TicTacToe Directory
 C:\jdk1.3\demo\applets\TicTacToe>dir  Volume in drive C has no label  Volume Serial Number is 07D0-0606  Directory of C:\jdk1.3\demo\applets\TicTacToe .              <DIR>        06-15-00  3:54p . ..             <DIR>        06-15-00  3:54p .. TICTAC~1 CLA         3,900  06-02-00  1:11p TicTacToe.class TICTAC~1 JAV         8,020  06-02-00  1:11p TicTacToe.java EXAMPL~1 HTM           424  06-02-00  1:11p example1.html AUDIO          <DIR>        06-15-00  3:54p audio IMAGES         <DIR>        06-15-00  3:54p images          3 file(s)         12,344 bytes          4 dir(s)       23,087.70 MB free C:\jdk1.3\demo\applets\TicTacToe> 

For purposes here, create a new JAR file containing everything in this directory and the entire audio and images subdirectories as well. Call the new JAR file ourtest.jar and create it in the current directory. You'll use this file later, so don't delete it right away.

Caution

Be careful when creating a new JAR file; if one already exists with the same name as the new one being created, the old one will be overwritten.


Type the following command and run it from the TicTacToe directory, which should be located at JAVA_HOME/demo/applets directory:

 jar cvf ourtest.jar * 

Notice that when directories are listed as input files to the JAR tool, their contents are added to the archive and the directory names are preserved. Also, notice that directories are processed recursively. You didn't have to tell it anything special to recursively process into the audio or images directory. Listing 25.2 shows the output when you run the command.

Listing 25.2 Creating a New JAR File
 C:\jdk1.3\demo\applets\TicTacToe>jar cvf ourtest.jar * added manifest adding: audio/(in = 0) (out= 0)(stored 0%) adding: audio/beep.au(in = 4032) (out= 3572)(deflated 11%) adding: audio/ding.au(in = 2566) (out= 2055)(deflated 19%) adding: audio/return.au(in = 6558) (out= 4401)(deflated 32%) adding: audio/yahoo1.au(in = 7834) (out= 6985)(deflated 10%) adding: audio/yahoo2.au(in = 7463) (out= 4607)(deflated 38%) adding: example1.html(in = 424) (out= 238)(deflated 43%) adding: images/(in = 0) (out= 0)(stored 0%) adding: images/cross.gif(in = 157) (out= 160)(deflated -1%) adding: images/not.gif(in = 158) (out= 161)(deflated -1%) adding: TicTacToe.class(in = 3900) (out= 2247)(deflated 42%) adding: TicTacToe.java(in = 8020) (out= 2971)(deflated 62%) C:\jdk1.3\demo\applets\TicTacToe> 

When the JAR tool creates a new archive, it automatically adds a manifest file to the archive. In most cases, this will suffice. However, if you need to add your own manifest file and have the JAR tool use that, you can do so by specifying the m option.

Listing Archive Contents

The JAR tool can also list the contents of a JAR archive without changing the contents. For example, run the following command from the directory where you created the ourtest.jar from before:

 jar tvf ourtest.jar 

It will list the contents of the ourtest.jar file that was just created. Note that you will have to run this command from the directory where the ourtest.jar was written. Listing 25.3 shows the contents of the JAR file.

Listing 25.3 Display of the JAR File Contents
 C:\jdk1.3\demo\applets\TicTacToe>jar tvf ourtest.jar      0 Thu Jul 13 22:24:18 PDT 2000 META-INF/     68 Thu Jul 13 22:24:18 PDT 2000 META-INF/MANIFEST.MF      0 Thu Jun 15 15:54:36 PDT 2000 audio/   4032 Fri Jun 02 13:11:22 PDT 2000 audio/beep.au   2566 Fri Jun 02 13:11:22 PDT 2000 audio/ding.au   6558 Fri Jun 02 13:11:22 PDT 2000 audio/return.au   7834 Fri Jun 02 13:11:22 PDT 2000 audio/yahoo1.au   7463 Fri Jun 02 13:11:22 PDT 2000 audio/yahoo2.au    424 Fri Jun 02 13:11:22 PDT 2000 example1.html 0 Thu Jun 15 15:54:36 PDT 2000 images/    157 Fri Jun 02 13:11:22 PDT 2000 images/cross.gif    158 Fri Jun 02 13:11:22 PDT 2000 images/not.gif   3900 Fri Jun 02 13:11:22 PDT 2000 TicTacToe.class   8020 Fri Jun 02 13:11:22 PDT 2000 TicTacToe.java C:\jdk1.3\demo\applets\TicTacToe> 

Notice that a manifest file has been added to the archive automatically. See the "Manifest File" section later in this chapter for more information.

Note

Because JAR files are based on ZIP files, you don't have to use the command line to view the contents of a JAR file. You can also use the WinZip tools to view a JAR file. Just be careful not to modify the contents or corrupt it in any way. This might cause problems for applications that need to read it later. Otherwise, it's a handy way to view contents.


Troubleshooting Tip

If you are having trouble with opening a JAR file using the WinZip application, see "Can't View a JAR File Using the WinZip Program" in the "Troubleshooting" section at the end of this chapter.


Extracting Files from an Archive

Finally, the JAR tool can extract files from an archive file. For example, to extract the TicTacToe.class file from the ourtest.JAR file, type the following:

 C:\jdk1.3\demo\applets\TicTacToe>jar xvf ourtest.jar TicTacToe.class extracted: TicTacToe.class C:\jdk1.3\demo\applets\TicTacToe> 

You can also use the x option to extract a single file within a subdirectory of the JAR archive. You just need to specify the full path . So if you wanted to extract just the cross.gif file from the images directory within the ourtest.JAR, you would need to specify the following command:

 C:\jdk1.3\demo\applets\TicTacToe>jar xvf ourtest.jar images/cross.gif extracted: images/cross.gif C:\jdk1.3\demo\applets\TicTacToe> 

Updating a JAR File

You can update the files within the JAR file by using the “u option on the jar command like this:

 jar uf jar-file input-file(s) 

input-file(s) is a space-separated list of one or multiple files that need to be added to the JAR file. If a file already exists in the JAR, it will be overwritten.

You can also update the manifest file by using the um option. For example, you can do the following to merge a new manifest file with an existing one:

 jar umf manifest jar-file 

The file manifest contains changes that you want to merge into the manifest that exists in the JAR-file. The next section gives more information about the manifest file.

   


Special Edition Using Java 2 Standard Edition
Special Edition Using Java 2, Standard Edition (Special Edition Using...)
ISBN: 0789724685
EAN: 2147483647
Year: 1999
Pages: 353

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