JAR Guide (much taken from the SUN page)


Why a JAR? (from Core Java Vol. 1)

Applets often makes use of multiple classes, as well as images, sounds, and other resources. Some might be from the core Java classes, some might be from other packages, other files (.gif and .au), etc. As you know, the applet tag references the class file that contains the class:
   <applet code="classname.class" WIDTH = 400 HEIGHT = 300> 
When the browser reads this line, it makes a connection to the Web server and fetches the file. The class loader of the Java interpreter that is built into the browser then loads the specified class from that file. During the loading process, the class loader must resolve the other classes used in the class.

After doing so (if an applet that uses multiple classes), it then knows it needs more classes to run the applet. The browser, then must make multiple (possibly many more) connections to the Web server, one for each class file. Even for a small applet, this can be a very time consuming process. Loading an applet that takes dozens of classes can take many minutes.

Java supports an improved method for loading class files, which allows you to package all the needed class files into a single file.

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:

JAR consists of a zip archive, as defined by PKWARE, containing a manifest file and potentially signature files, as defined in the Manifest and Signature specification. (see below)

How to make a JAR

A JAR archive can be created with the jar tool shipped with the JDK. See Java in a Nutshell page 246 (or Core Java Vol1 pg. 501) for the specification of the Java ARchive Tool and a list of options for the command:


jar c|t|x[f][m][v] [jar-file] [manifest-file] [files]


Here is a page with the jar options

Once you have created a JAR file, you refer to it in the <APPLET> tag with the archive attribute.

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 
      width=460 height=160>
      <param name=foo value="bar">
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.

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">
There can be any amount of white space between entries within the archive parameter. In addition, the archive tag itself is case-insensitive; it can be lower-case, upper-case, or any combination of lower- and upper-case letters, such as ArCHiVe.

Access files in jars from command line/executable files

See the tools docs to see how to call .jar files from the command line. Specifically...
Here are the java command options (note the use of -jar) which mentions the use of the JAR Manifest for using -jar. If you do not like reading the formal documentation, here is a page I made to show you how to make an executable jar

Or, you can just look at the jar tutorial section on this topic.

Beans and Manifest files

The Manifest is stored in a file called META-INF/MANIFEST.MF and contains meta information about the files in the archive. By default, the jar creates a manifest file that contains MD5 and SHA message digests for each file in the archive. This information can be used by the appletviewer or Web browser to verify that the files have not been corrupted since the JAR file was created. See the Manifest and Signature specification for more information.

To prepare a Bean for a Beanbox, you must package it up in a JAR file along wiht any other classes or resources it requires. The manifest of the JAR must define which of the JAR file entries are beans.

When you use the m option in conjunction with c, it tells jar to read a partial manifest file that you specify. This file, perhaps named manifest.stub should have contents:

Warning: The text file must end with a new line or carriage return. The last line will not be parsed properly if it does not end with a new line or carriage return.

(Note Nutshell, page 183 for directory information)

Each bean must be specified as such in the manifest.

Finally: A minimul bean and its JAR file

Your minimal Bean will be named SimpleBean. Here are the steps to create it and view it in the BeanBox:

  1. Write the SimpleBean code. Put it in a file named SimpleBean.java, in the directory of your choice. Here's the code:
    import java.awt.*;
    import java.io.Serializable;
    public class SimpleBean extends Canvas
                         implements Serializable{
      //Constructor sets inherited properties
      public SimpleBean(){

    SimpleBean extends the java.awt.Canvas component. SimpleBean also implements the java.io.Serializable interface, a requirement for all Beans. Setting the background color and component size is all that SimpleBean does.

  2. Make sure the CLASSPATH environment variable is set to point to all needed .class (or .jar) files.

  3. Compile the Bean:
          javac SimpleBean.java
    This produces the class file SimpleBean.class

  4. Create a manifest file. Use your favorite text editor to create a file (we'll call it manifest.tmp), that contains the following text:
         Name: SimpleBean.class
         Java-Bean: True
  5. Create the JAR file. The JAR file will contain the manifest and the SimpleBean class file:
         jar cfm SimpleBean.jar manifest.tmp SimpleBean.class
    The Java Sun website contains documentation on JAR files.

  6. Load the JAR file into the ToolBox. Pull down the File|LoadJar... menu item. This will bring up a file browser. Navigate to the SimpleBean.jar location and select it. SimpleBean will appear at the bottom of the ToolBox. (Note that when the BeanBox is started, all Beans in JAR files in the beans/jars directory are automatically loaded into the ToolBox).

  7. Drop a SimpleBean instance into the BeanBox. Click on the word SimpleBean in the ToolBox. The cursor will change to crosshairs. Move the cursor to a spot within the BeanBox and click. SimpleBean will appear as a painted rectangle with hatched border. This border means that SimpleBean is selected. The SimpleBean properties will appear in the Properties sheet.