The JAR file format is a platform-independent format for compressing, packaging, and delivering several files together. Based on the ZIP file format and the ZLIB compression standards, the JAR ( Java archive) packages and tool were originally developed to make downloads of Java applets more efficient. As a packaging mechanism, however, the JAR file format is a convenient way to “shrink-wrap” components and other software for delivery to third parties. The original JavaBeans component architecture depends on JAR files for packaging, as does Enterprise JavaBeans. The goal in using the JAR file format is to package all the classes and interfaces associated with one or more beans, including the deployment descriptor, into one file.
The JAR file is created using a vendor-specific tool, or using the jar utility that is part of the Java 2, Standard Edition development kit. An ejb-jar file contains:
The XML deployment descriptor
The bean classes
The remote and home interfaces
The primary key class
Dependent classes and interfaces
The XML deployment descriptor must be located in the path
META-INF/ejb-jar.xml and must contain all the
deployment information for all the beans in the
ejb-jar file. For each bean declared in the XML
deployment descriptor, the ejb-jar file must
contain its bean class, remote and home interfaces, and dependent
classes and interfaces. Dependent classes and interfaces are usually
things like application-specific exceptions, business interfaces and
other supertypes, and dependent objects that are used by the bean. In
the ejb-jar file for the TravelAgent bean, for
example, we would include the
IncompleteConversationalState
application
exception and the Ticket
and
CreditCard
classes, as well as the remote and home
interfaces to other beans referenced by the TravelAgent bean, such as
the Customer and ProcessPayment beans.[62]
You can use the jar utility from the command line to package a bean in a JAR file. Here is an example of how the jar utility was used to package the Cabin EJB in Chapter 4:
dev % jar cf cabin.jar com/titan/cabin/*.class META-INF/ejb-jar.xml F:..dev>jar cf cabin.jar com itancabin*.class META-INFejb-jar.xml
You might have to create the
META-INF
directory first, and copy ejb-jar.xml into that
directory. The c option tells the
jar utility to create a new JAR file that
contains the files indicated in subsequent parameters. It also tells
the jar utility to stream the resulting JAR file
to standard output. The f option tells
jar to redirect the standard output to a new
file named in the second parameter (cabin.jar).
It is important to get the order of the option letters and the
command-line parameters to match. You can learn more about the
jar utility and the
java.util.zip
package in Java™ in a Nutshell by David Flanagan or Learning
Java™ by Pat Niemeyer and Jonathan Knudsen, both
published by O’Reilly.
The jar utility creates the file cabin.jar in the dev directory. If you are interested in looking at the contents of the JAR file, you can use any standard ZIP application (WinZip, PKZIP, etc.) or the command jar tvf cabin.jar.
EJB 1.1 also allows for a client-jar file, which includes only the interfaces and classes needed by a client application to access a bean. This includes the remote and home interfaces, primary key, and any dependent types to which the client is exposed, such as application exceptions. The specification does not say how this file is delivered to the client, what exactly it contains, or how it is packaged with the ejb-jar file. In other words, the client-jar file is a fairly vendor-specific concept in EJB.
[62] The EJB 1.1
specification also allows remote and home interfaces of referenced
beans to be named in the manifest’s
Class-Path
attribute, instead of including them in
the JAR file. Use of the Class-Path
entry in the
JAR’s manifest is addressed in more detail in the Java 2,
Standard Edition specification.