Home

About Me

Interest

Favorites

Photo Gallery

Programs

  • Jar files

JAR - Java ARchive

What is Jar?

Jar stands for Java ARchive. It's a file format based on the popular ZIP file format and is used for aggregating many files into one. Although Jar can be used as a general archiving tool, the primary motivation for its development was so that Java applets and their requisite components (.class files, images and sounds) can be downloaded to a browser in a single HTTP transaction, rather than opening a new connection for each piece. This greatly improves the speed with which an applet can be loaded onto a web page and begin functioning. The Jar format also supports compression, which reduces the size of the file and improves download time still further. Additionally, individual entries in a Jar file may be digitally signed by the applet author to authenticate their origin.

Jar is:

  • the only archive format that is cross-platform
  • the only format that handles audio and image files as well as class files
  • backward-compatible with existing applet code
  • an open standard, fully extendable, and written in java
  • the preferred way to bundle the pieces of a java applet

Jar consists of a zip archive, as defined by PKWARE, containing a manifest file and potentially signature files

The APPLET tag

Changing the APPLET tag in your HTML page to accomodate a Jar file is simple. The Jar file on the server is identified by the ARCHIVE parameter, where the directory location of the jar file should be relative to the location of the html page:

    <applet code=Animator.class 
      archive="jars/animator.jar"
      width=460 height=160>
      <param name=foo value="bar">
    </applet>

Note that the familiar CODE=myApplet.class parameter must still be present. The CODE parameter, as always, identifies the name of the applet where execution begins. However, the class file for the applet and all of its helper classes are loaded from the Jar file.

The ARCHIVE attribute describes one or more Jar files containing classes and other resources that will be "preloaded". The classes are loaded using an instance of an AppletClassLoader with the given CODEBASE. It takes the form archive = archiveList. The archives in archiveList are separated by ",".

Once the archive file is identified, it is downloaded and separated into its components. During the execution of the applet, when a new class, image or audio clip is requested by the applet, it is searched for first in the archives associated with the applet. If the file is not found amongst the archives that were downloaded, it is searched for on the applet's server, relative to the CODEBASE (that is, it is searched for as in JDK1.0.2).

The archive tag may specify multiple Jar files. Each Jar file must be separated by "," (comma). Each file is downloaded in turn:

    <applet code=Animator.class 
      archive="classes.jar ,  images.jar ,  sounds.jar"
      width=460 height=160>
      <param name=foo value="bar">
    </applet>

Executable Jar Files

On Microsoft Windows systems, the Java 2 Runtime Environment's installation program will register a default association for Jar files so that double-clicking a Jar file on the desktop will automatically run it with javaw -jar. Dependent extensions bundled with the application will also be loaded automatically. This feature makes the end-user runtime environment easier to use on Microsoft Windows systems.

The Solaris 2.6 kernel has already been extended to recognize the special "magic" number that identifies a Jar file, and to invoke java -jar on such a Jar file as if it were a native Solaris executable. A application packaged in a Jar file can thus be executed directly from the command line or by clicking an icon on the CDE desktop.

 

Here I begin......

So by now you must have got familiar with the Jar file concept. You can get more information about Jar files from www.java.sun.com and if u have the java doc. with u well and good, check this directory DOCS\GUIDE\JAR. Now let me show you how to create a Jar file.

Go to the installation directory of java in your system. there within the BIN directory you will find a executable file called "jar.exe". This is the utility program that is needed to make a jar file. Jar files have extensions ".jar". The different ways to create and manipulate Jar files are given below. Below you can find a new term called manifest, I Think it would be better to throw some light on it now itself. Its a text file with name "MANIFEST.MF". It contains additional data about the Jar file. The manifest file is the place where any meta-information about the archive is stored as name: value pairs. 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.

Create jar file
jar c[v0M]f jarfile [-C dir] inputfiles [-Joption]
jar c[v0]mf manifest jarfile [-C dir] inputfiles [-Joption]
jar c[v0M] [-C dir] inputfiles [-Joption]
jar c[v0]m manifest [-C dir] inputfiles [-Joption]

 

Update jar file
jar u[v0M]f jarfile [-C dir] inputfiles [-Joption]
jar u[v0]mf manifest jarfile [-C dir] inputfiles [-Joption]
jar u[v0M] [-C dir] inputfiles [-Joption]
jar u[v0]m manifest [-C dir] inputfiles [-Joption]

 

Extract jar file
jar x[v]f jarfile [inputfiles] [-Joption]
jar x[v] [inputfiles] [-Joption]

 

List table of contents of jar file
jar t[v]f jarfile [inputfiles] [-Joption]
jar t[v] [inputfiles] [-Joption]

 

Add index to jar file
jar i jarfile [-Joption]

where:

cuxtiv0Mmf
Options that control the jar command.
jarfile
Jar file to be created (c), updated (u), extracted (x), or have its table of contents viewed (t). The f option and filename jarfile are a pair -- if either is present, they must both appear. Note that omitting f and jarfile accepts a "jar file" from standard input (for x and t) or sends the "jar file" to standard output (for c and u).
inputfiles
Files or directories, separated by spaces, to be combined into jarfile (for c and u), or to be extracted (for x) or listed (for t) from jarfile. All directories are processed recursively. The files are compressed unless option O (zero) is used.
manifest
Pre-existing manifest file whose name: value pairs are to be included in MANIFEST.MF in the jar file. The m option and filename manifesfile are a pair -- if either is present, they must both appear. The letters m and f must appear in the same order that manifest and jarfile appear.
-C dir
Temporarily changes directories to dir while processing the following inputfiles argument. Multiple -C dir inputfiles sets are allowed.
-Joption
Option to be passed into the Java runtime environment. (There must be no space between -J and option).

OPTIONS

c
Creates a new archive to file named jarfile (if f is specified) or to standard output (if f and jarfile are omitted). Add to it the files and directories specified by inputfiles.
u
Updates an existing file jarfile (when f is specified) by adding to it files and directories specified by inputfiles. For example:
jar uf foo.jar foo.class

would add the file foo.class to the existing jar file foo.jar. The u option can also update the manifest entry, as given by this example:

jar umf manifest foo.jar

updates the foo.jar manifest with the name: value pairs in manifest.

x
Extracts files and directories from jarfile (if f is specified) or standard input (if f and jarfile are omitted). If inputfiles is specified, only those specified files and directories are extracted. Otherwise, all files and directories are extracted.
t
Lists the table of contents from jarfile (if f is specified) or standard input (if f and jarfile are omitted). If inputfiles is specified, only those specified files and directories are listed. Otherwise, all files and directories are listed.
i
Generate index information for the specified jarfile and its dependent jar files. For example:
jar i foo.jar

would generate an INDEX.LIST file in foo.jar which contains location information for each package in foo.jar and all the jar files specified in the Class-Path attribute of foo.jar. See the index example.

f
Specifies the file jarfile to be created (c), updated (u), extracted (x), indexed (i), or viewed (t). The f option and filename jarfile are a pair -- if present, they must both appear. Omitting f and jarfile accepts a "jar file" from standard input (for x and t) or sends the "jar file" to standard output (for c and u).
v
Generates verbose output to standard output. Examples shown below.
0
(zero) Store without using ZIP compression.
M
Do not create a manifest file entry (for c and u), or delete a manifest file entry if one exists (for u).
m
Includes name: value attribute pairs from the specified manifest file manifest in the file at META-INF/MANIFEST.MF. A name: value pair is added unless one already exists with the same name, in which case its value is updated.

On the command line, the letters m and f must appear in the same order that manifest and jarfile appear. Example use:

jar cmf myManifestFile myFile.jar *.class

You can add special-purpose name: value attribute pairs to the manifest that aren't contained in the default manifest. Examples of such attributes would be those for vendor information, version information, package sealing, and to make JAR-bundled applications executable.

I know that the above document is still very bookish that's because most of it are excerpts from books or sun's documents. I think it would be better if we go through some examples now.

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".

If you have a pre-existing manifest file whose name: value pairs you want the jar tool to include for the new jar archive, you can specify it using the m option:

C:\Java> jar cmf myManifestFile myFile.jar *.class

Be sure that any pre-existing manifest file that you use ends with a new line. The last line of a manifest file will not be parsed if it doesn't end with a new line character. Note that when you specify "cfm" instead of "cmf" (i.e., you invert the order of the "m" and "f" options), you need to specify the name of the jar archive first, followed by the name of the manifest file:

C:\Java> jar cfm myFile.jar myManifestFile *.class

The manifest is in a text format inspired by RFC822 ASCII format, so it is easy to view and process manifest-file contents.

To extract the files from a jar file, use x, as in:

C:\Java> jar xf myFile.jar

To extract only certain files from a jar file, supply their filenames:

C:\Java> jar xf myFile.jar foo bar

Some more real life examples:

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

 

Here comes another example. According to me this is one of the most important ones. At the start of this topic we have learnt about executable Jar files, but until now we haven't discussed about it. I think it is something of real interest to all Java application developers. If you ever go through the examples provided by Sun. All of them have Jar files, and when you click on them the application gets started. Its look somewhat similar to exe files of windows. Here we begin.

1. Write a simple java program. The one I wrote was....

import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;

class myWin extends JFrame
{     public myWin(String s)
      {   super(s);
           Toolkit kit = getToolkit();
           Dimension scrn = kit.getScreenSize();
           setBounds(scrn.width/2-(scrn.width/4),scrn.height/2-(scrn.height/4),
                 scrn.width/2,scrn.height/2);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            setVisible(true);
      }
};

class GUI_test
{   public static void main(String[] args)
    {    myWin win = new myWin("Swing Test");
    }
}

Name the program "GUI_test.java"

2. Compile the program using  "c:\> javac GUI_test.java"  this will result in two class files
     a) myWin.class and b) GUI_test.class

3. Copy this 2 class files into the BIN directory if PATH is not set to Bin directory of java.
    Next run jar as shown:     jar cvf app.jar *.class
           This command will create a jar file called app.jar which will include all the class files
           in the directory. So keep in mind that the directory should contain only the above 2
           class files and no other.

4. The output of the above command will be something like this:
          added manifest
          adding: GUI_test.class(in = 336) (out= 248)(defaulted 26%)
          adding: myWin.class(in = 2469) (out= 1361)(defaulted 44%)
   And we will get a Jar file name app.jar. Next we will extract the contents of the jar file
   for this remove the class files present in the directory and execute the command,

                              jar xvf app.jar

 
This will extract the class files within app.jar along with the manifest.mf file which will be
  extracted within a directory called META-INF. This new directory will be created within
  the current directory.

5. Open the manifest.mf file in notepad. the content of it in my system is :
                          Manifest-Version: 1.0
                          Created-By: 1.4.2_03 (Sun Microsystems Inc.)

    At the end of the file append the following line and press enter. Then save it.
                          Main-Class: boxGUI_1

6. Then copy the file manifest.mf to the folder above it. Now we will have all the 3 files in
    in the sane place i.e.
                     a) myWin.class and b) GUI_test.class and c) manifest.mf

7. Next execute this command :
                     jar cmf manifest.mf app.jar *.class

8. At last click on the Jar file. The application will automatically start. Did it worked ? ? ?

 

 

 
     

Home | About me | Interest | Favorites | Photo Gallery | Programs

Hosted by www.Geocities.ws

1