Java Archive Files

Java archive files are flat, serialized files that maintain the hierarchical structures of (nested) directories and/or java packages.  Since they are serialized, they can used to distribute applications and libraries via the network and for deployment to a server.

There are three types of Java archives that are important for Web-based systems:

 Java Archives (jar files) are usually hierarchies of Java class and, sometimes, source files.  Typical examples include libraries, such as Struts or a dbms server's drivers.  They are also frequently used to deploy the various files required by an Applet.

Web Archives (war files) are hierarchies of both Java classes but also other files such as HTML or JSP pages that serve as view objects.  While they can be used for distribution, they are normally used to deploy an application to a Web container. 

Enterprise Archives (ear files) are used to package so-called Enterprise applications, normally intended to be run on enterprise or application servers such as WebSphere or WebLogic.  While they can include basic elements, such as directory and package hierarchies, usually they include such components as already compressed jar and war files.  Thus, an enterprise application is likely to have a war file for its view and controller layers and separate jar files for its Session EJB, domain model, and Entity EJB layers, as well as other jars for databeans, utility classes, and other libraries it may rely on.

Whereas the Java development kit includes a jar command that can be used to create such files from a command line, they are normally created within a development environment, such as Eclipse or IBM's Studio environments (WebSphere or Rational).  Consequently, in the discussions below a brief example of creating a jar file manually will be given, but war and ear file creation will be limited to doing so from within one of the environments.


JAR Files

JARs are a special form of Zip compressed file.  They allow the Java a programmer to group a set of related classes into a single file that can be copied, moved, and/or passed around the network.  One particularly noteworthy instance of this is downloading the files associated with an Applet.  If the files are contained "loose" within a directory, each is loaded separately and each requires a round trip between client/browser and server.  However, if several applet-related files are gathered into a jar file, the entire jar file is loaded in a single fetch.

JAR files allow each file contained within them to be identified, and they retain any inherent relationships that exist among the files, such as inclusion in several levels of hierarchical directories.  Thus, for example, one might have a directory structure such as the following:

Project
  applets
    ui.class
    addressbookfacade.class
    communicator.class
  servlets
    controlservlet.class
    listenserver.class
    handleserver.class
    adddressbook.class

Jar files can be created automatically by many Java development environments.  However, the JDK/SDK distributions also include a jar management tool, called not surprisingly jar. Review the documentation, paying particular attention to the basic syntax of the command.

Two basic forms of the command that are likely to be used rather often are:

    jar cf MyJarFile.jar *.class
    
and 
    jar uf MyJarFile.jar anotherclass.class
    

The first creates a new jar file, MyJarFile.jar, from all of the .class files in the current directory.  If MyJarFile.jar exists, it is overwritten.

The second adds an additional file -- anotherclass.class -- to an existing jar file, updating it but not destroying the original.

If the files are included within several layers of directories, they may all be included by referring simply to the directory name.  However, one must be cautious that the directory structure reflects the package (hierarchical) name for the files since they will be accessible from within the jar file by the concatenated names of the directories. 

The files in the jar file example could then be referenced through the applet tag:

<applet 
	code=myApplet.class 
	codebase=path/to/code/directory 
	archive=MyJarFile.jar >
</applet>

A far easier way to create jar files, if you are using an integrated development environment such as Eclipse, is to do so directly from within the IDE.  For example, Eclipse includes an export command that will take you through a sequence of steps where you may select the specific project files to include in a jar file and a directory location in which to place the newly created jar.


WAR Files

War files are Web Archives that may include all of the files associated with a Web applications, such as HTML and JSP view pages, but also Java classes such as servlets, framework files, dbms drivers, etc.  They are more complex to create, so they will not be described outside of the context of an IDE.  The specific IDE that will be discussed here is Eclipse v. 3.

To create a war file, you need to do the following:

  1. Right-click on the project and drill down to properties>tomcat>Export to WAR Settings.
  2. Specify the path and file name for the .war file to be created for this project.
  3. After doing the above, again right-click on the project and drill down to Tomcat project>Export to the war file . . .  This will create a war file in the location and with the name specified in step 2, above.

In order to deploy the war file to a server, you need to have direct and administrative access to the server.  Thus, it may not be practical to use war files to deploy applications for course projects; consequently, the remainder of this discussion assumes you are exploring these ideas with respect to a server you control.

An application deployed as a war file is normally deployed to the server's general webapps directory.  But first you must confirm several parameters in the host tag in the server.xml file.  A typical configuration might look like this:

<Host name="localhost" 
	appBase="webapps"
       	unpackWARs="true" 
	autoDeploy="true"
       	xmlValidation="false" 
	xmlNamespaceAware="false">

These parameters will insure that any war file placed in this directory will be unpacked a short time after it is dropped there and its contents deployed and enabled for access.


EAR Files

Ear files are Enterprise Archives that normally include war and jar files the comprise the various layers and components of the application.  They are still more complex to create, so they will not be described outside of the context of an IDE.  The specific IDE that will be discussed here is WebSphere Studio (WAS).

the process of creating an ear file in WAS is deceptively simple.  Just right click on the enterprise project and drill down to export>EAR file and follow the prompts.  Deployment is specific to the application server, so the process will be demonstrated but not described here.  See the documentation for the particular server you are using.