The JAR File

JAR stands for Java ARchive, although it seems to be an obvious pun on the fact that Java components are called beans ("a jar of beans"). A JAR file is a collection of other files. JAR files use the ZIP file format, which supports compression. Many components, subsystems, and modules are deployed in JAR files or some variation of a JAR file. There are JAR files for JavaBeans and applets. Other JAR files contain libraries of Java code. There are JAR files that contain Enterprise JavaBeans. Then there are variations of JAR files that contain other JAR files and JavaServer Pages (JSPs) like the WAR files. The king of all JAR files, the EAR file, can contain other JAR files, EJB JAR files, and WAR files. Each JAR file contains a manifest file called MANIFEST.MF, which is located in the JAR file’s META-INF directory. The manifest file is a text file that consists of entries that describe the contents of the JAR file. The main section of the manifest file contains configuration information and specifies the app or extension the JAR file is associated with. JAR files were originally developed to package Java applets and their requisite components (.class files, images, and sounds). The boon to applet development was that the applet could be downloaded to a browser in a single file, so the browser didn’t have to download many files—a major boost to efficiency and speed.

JAR Files and Applets

For applets, JAR files are specified in the HTML applet tag’s “archive” parameter. This parameter causes the browser to load the JAR file, which contains the applet code. For example:

 <applet code= "xptoolkit.applet.HelloWorldApplet" archive="helloapplet.jar"
 width=200 height=200>

Applet delivery has complex problems and solutions. It’s supposed to be easy to request a browser to open a JAR file. The problem is that one major Web browser vendor decided not to support Java any longer. The vendor froze its version of Java at version JDK 1.1.5. Thus, you can use the applet tag we just described if you do not use any features of Java newer than JDK 1.1.5. Another major vendor of Web browsers supported the later versions of Java, but at the time its browser support of Java was not very robust. Thus, Sun had two choices: It could wait for the browser to catch up or write its own Java plug-in for the major browsers. The good folks at Sun decided to write their own plug-in for Java. A tag that supports both Netscape and Microsoft may look like this:

 <object classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93" codebase=",2,2,0" align="center">
 <param name="java_code" value="xptoolkit.applet.HelloWorldApplet">
 <param name="java_archive" value="helloapplet.jar">
 <param value="app/x-java-applet">
 <embed type="app/x-java-applet" codebase="" align="center" java_code="xptoolkit.applet.HelloWorldApplet" java_archive="helloapplet.jar">
 </comment> <p> Java is cool. Get a browser that supports the plugin. </ br>

The embed tag is for Netscape Navigator, and the “java_archive” parameter specifies the JAR file that contains the applet code. The object tag works with Microsoft Internet Explorer, and it uses the "java_archive" parameter to specify the JAR file. JSP has a plug-in tag that helps simplify this issue for you. The plug-in tag specifies the JAR file that the applet needs in its "archive" parameter. It is demonstrated as follows:

<jsp:plugin type="applet" code="xptoolkit.applet.HelloWorldApplet" archive="helloapplet.jar"
 <jsp:fallback> <!-- This fallback message will display if the plugin does not work. /-->
 <p> Java is cool. Get a browser that supports the plugin. </ br>
 Or we will hunt you down and melt your computer!

An example of using this technique to launch applets appears in .

Executable JAR Files

In addition to supporting applets, Java supports JAR files so that double-clicking a JAR file (or the equivalent gesture on your OS) will automatically run the app in the JAR file. In order to do this, you must specify the name of the app’s startup class in the JAR manifest file. The startup class is called the main class. You can run a JAR file that specifies a main class as follows:

C:\tmp\app\lib> java -jar greetapp.jar

To specify a main class for a JAR file, you specify the "Main-Class" attribute in the JAR manifest file:

Main-Class : xptoolkit.HelloWorld

An example of how to create an executable JAR file with Ant appears in .

This was just a brief introduction to JAR files. Later chapters contain plenty of examples that show you how to use Ant to build the various distribution files based on the JAR format you need and use.