Programming Tutorials

Using jar utility in Java - A jar tutorial

By: Emiley J in Java Tutorials on 2007-10-14  

The jar tool combines multiple files into a single JAR archive file. jar is a general-purpose archiving and compression tool, based on ZIP and the ZLIB compression format. However, jar was designed mainly to facilitate the packaging of java applets or applications into a single archive. When the components of an applet or application (.class files, images and sounds) are combined into a single archive, they may be downloaded by a java agent (like a browser) in a single HTTP transaction, rather than requiring a new connection for each piece. This dramatically improves download times. jar also compresses files and so further improves download time. In addition, it allows individual entries in a file to be signed by the applet author so that their origin can be authenticated. The syntax for the jar tool is almost identical to the syntax for the tar command. A jar archive can be used as a class path entry, whether or not it is compressed.

Typical usage to combine files into a jar file is:

C:\Java> jar cf myFile.jar *.class

In this example, all the class files in the current directory are placed into the file named "myFile.jar". A manifest file entry named META-INF/MANIFEST.MF is automatically generated by the jar tool and is always the first entry in the jar file. The manifest file is the place where any meta-information about the archive is stored as name : value pairs.

To add all the files in a particular directory to an archive (overwriting contents if the archive already exists). Enumerating verbosely (with the v option) will tell you more information about the files in the archive, such as their size and last modified date.

C:\Java> dir

12/09/96  12:20a        <DIR>          .    
12/09/96  12:17a        <DIR>          ..  
12/09/96  12:18a                   946 1.au
12/09/96  12:18a                 1,039 2.au
12/09/96  12:18a                   993 3.au
12/09/96  12:19a                48,072 spacemusic.au
12/09/96  12:19a                   527 at_work.gif
12/09/96  12:19a                12,818 monkey.jpg
12/09/96  12:19a                16,242 Animator.class
12/09/96  12:20a                 3,368 Wave.class

              10 File(s)        91,118 bytes

C:\Java> jar cvf bundle.jar *

adding manifest
adding: 1.au
adding: 2.au
adding: 3.au
adding: Animator.class
adding: Wave.class
adding: at_work.gif
adding: monkey.jpg
adding: spacemusic.au

If you already have separate subdirectories for images, audio files and classes, you can combine them into a single jar file:

C:\Java> dir

12/09/96  12:11a        <DIR>          .
12/09/96  12:17a        <DIR>          ..
12/03/96  06:54p        <DIR>          audio
12/06/96  02:02p        <DIR>          images
12/09/96  12:10a        <DIR>          classes

               5 File(s)        207,360 bytes

C:\Java> jar cvf bundle.jar audio classes images

adding: audio/1.au
adding: audio/2.au
adding: audio/3.au
adding: audio/spacemusic.au
adding: classes/Animator.class
adding: classes/Wave.class
adding: images/monkey.jpg
adding: images/at_work.gif

C:\Java> dir 

12/09/96  12:11a        <DIR>          .
12/09/96  12:17a        <DIR>          ..
12/09/96  12:11a               207,360 bundle.jar
12/03/96  06:54p        <DIR>          audio
12/06/96  02:02p        <DIR>          images
12/09/96  12:10a        <DIR>          classes

               6 File(s)        207,360 bytes

To see the entry names in the jarfile, use the "t" option:

C:\Java> jar tf bundle.jar

META-INF/
META-INF/MANIFEST.MF
audio/1.au
audio/2.au
audio/3.au
audio/spacemusic.au
classes/Animator.class
classes/Wave.class
images/monkey.jpg
images/at_work.gif

To add an index file to the jar file for speeding up class loading, use the -i option.

Let's say you split the inter-dependent classes for a stock trade application, into three jar files:  main.jar, buy.jar, and sell.jar. If you specify the Class-path attribute in the main.jar manifest as:

Class-Path: buy.jar sell.jar

then you can use the -i option to speed up your application's class loading time:

C:\Java> jar i main.jar

An INDEX.LIST file is inserted to the META-INF directory which will enable the application class loader to download the specified jar files when it is searching for classes or resources.






Add Comment

* Required information
1000

Comments

No comments yet. Be the first!

Most Viewed Articles (in Java )

Latest Articles (in Java)