Java Beans   «Prev  Next»
Lesson 5 Packaging Beans in JAR files
ObjectiveUse the JAR utility to package a Bean in a JAR file.

Packaging Beans in JAR files

Packaging Beans in JAR files

Packaging JavaBeans components into JAR files has a significance far greater than just being able to test Beans in the BeanBox. Since JAR files form the standard means of distributing Beans, all application builder tools expect Beans to be packaged in JAR files. In other words, without packaging a Bean in a JAR file, it is unlikely that you will be able to use it in any visual development environment. Sure, you can still use it as any other Java class in straight hand-coded Java code, but that kind of defeats the purpose of JavaBeans. You should consider the packaging of Beans in JAR files as a standard part of the Bean construction process.
Fortunately, it is such a simple task thanks to the JAR utility that it does not require much of an effort. Besides, there is an organizational advantage to having all of the classes and resources of a Bean packaged into one file.

The ARCHIVES attribute is used in the HTML file to indicate that an archive file is used. The filename is relative to the location of the HTML page. Let's look at the HTML file we used for running the PickleUser applet from the previous chapter:

<APPLET
ARCHIVE="BeansBook.jar"
CODE=PickleUser.class
WIDTH=350
HEIGHT=125>
</APPLET>


The ARCHIVES attribute specifies that the BeansBook.jar archive file should be downloaded. Class files will be searched for in the specified archive first. If the class is not found in the archive, then the normal mechanism of searching the server and local file system will be used. Notice that the CODE attribute is still used to specify the starting class for the applet. When you run this HTML file with appletviewer, the PickleUser class will be loaded from the archive. The applet instantiates a pickled version of the BeansBook.util.PickleButton class, which is found inside the archive as BeansBook/util/SourPickle.ser.

Following is an example of how a Bean is packaged using the JAR utility:
C:\>jar fcm MyBean.jar MyBean.mf *.class 

fcm

In this example:
  1. The f option is used to specify the name of the new JAR file to be created, MyBean.jar.
  2. The c option indicates that a new JAR file is to be created containing all files matching the *.class wildcard. (All Java classes in the current directory, including the JavaBeans component class.)
  3. Finally, the m option specifies that an external manifest file, MyBean.mf, is to be included as the manifest file for the JAR file.

The last part of the JAR file creation is critical because JAR files containing Beans must have a manifest file identifying the Beans.
You may recall that a manifest file is just a text file with information about what Beans are included in a JAR file.

MyBean manifest file

Below is the MyBean manifest file, with a description of each code line:

 JAR version used to create the archive
1)JAR version used to create the archive

 Name of the JavaBean class
2)Name of the JavaBean class

 Flag specifying that this class is a Bean
3)Flag specifying that this class is a Bean



Javabeans manifest version:

1: Manifest-Version: 1.0
2: (Blank)
3: Name: MyBean.class
4: Java-Bean: True
Line 1: JAR version used to create the archive
Line 2: (Blank)
Line 3: Name of the JavaBeans class
Line 4: Flag specifying that this class is a Bean

Specifying a Manifest File

The jar program allows you to specify manifest information for the archive. You do not have to specify manifest information for every entry in the archive, or even specify all of the attributes for each entry. jar uses the information you provide in a manifest file, and generates information for the rest of the entries for you. You do, however, need to create entries in the manifest if you want to include attribute/value pairs other than the defaults (name and hash entries). We need to provide explicit manifest entries in order to identify any Beans in our JAR file by using the Java-Bean attribute with a value of True.
For clarity, I create entries for classes that are Beans, as well as those that are not. When you are creating your JAR files, remember to include all of the class files, not just those that implement your Beans. My own rule is to include every class and resource file for thepackages in the archive. Remember that you are not limited to class files; you can also include images, audio files, and any other resource files your package needs. Take care not to forget any support classes, in particular, any class files that are generated automatically by the compiler (for example, class files for inner classes).


Packaging Javabeans - Exercise

Click the Exercise link below to use the JAR utility to package the FancyButton Bean.
Packaging Javabeans - Exercise
In the next lesson, the BeanBox is used to test the FancyButton Bean.