14.4 Packaging a Bean



Before you can bring a Bean into a Bean-enabled visual builder tool, it must be put into the standard Bean container, which is a JAR (Java ARchive) file that includes all the Bean classes as well as a "manifest" file that says "This is a Bean." A manifest file is simply a text file that follows a particular form. For the BangBean, the manifest file looks like this: The first line indicates the version of the manifest scheme, which until further notice from Sun is 1.0. The second line (empty lines are ignored) names the BangBean.class file, and the third says, "It's a Bean." Without the third line, the program builder tool will not recognize the class as a Bean.

The only tricky part is that you must make sure that you get the proper path in the "Name:" field. If  it's in package such as named bangbean (and thus in a subdirectory called "bangbean" that's off of the classpath), and the name in the manifest file must include this package information. In addition, you must place the manifest file in the directory above the root of your package path, which in this case means placing the file in the directory above the "bangbean" subdirectory. Then you must invoke jar from the same directory as the manifest file, as follows:

This assumes that you want the resulting JAR file to be named BangBean.jar and that you've put the manifest in a file called BangBean.mf.

You might wonder "What about all the other classes that were generated when I compiled BangBean.java?" Well, they all ended up inside the bangbean subdirectory, and you'll see that the last argument for the above jar command line is the bangbean subdirectory. When you give jar the name of a subdirectory, it packages that entire subdirectory into the jar (including, in this case, the original BangBean.java source-code file -- you might not choose to include the source with your own Beans). In addition, if you turn around and unpack the JAR file you've just created, you'll discover that your manifest file isn't inside, but that jar has created its own manifest file (based partly on yours) called MANIFEST.MF and placed it inside the subdirectory META-INF (for "meta-information"). If you open this manifest file you'll also notice that digital signature information has been added by jar for each file, of the form:

In general, you don't need to worry about any of this, and if you make changes you can just modify your original manifest file and re-invoke jar to create a new JAR file for your Bean. You can also add other Beans to the JAR file simply by adding their information to your manifest.

One thing to notice is that you'll probably want to put each Bean in its own subdirectory, since when you create a JAR file you hand the jar utility the name of a subdirectory and it puts everything in that subdirectory into the JAR file. You can see that both Frog and BangBean are in their own subdirectories.

Once you have your Bean properly inside a JAR file you can bring it into a Beans-enabled program-builder environment. The way you do this varies from one tool to the next, but Sun provides a freely-available test bed for Java Beans in their "Beans Development Kit" (BDK) called the "beanbox." (Download the BDK from www.javasoft.com.) To place your Bean in the beanbox, copy the JAR file into the BDK's "jars" subdirectory before you start up the beanbox.


The End