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 files 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 application 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 didnt have to download many filesa major boost to efficiency and speed.

JAR Files and Applets

For applets, JAR files are specified in the HTML applet tags 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> 

Applet delivery has complex problems and solutions. Its 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="http://java.sun.com/products/plugin/1.2.2/jinstall-1_2_2- win.cab#Version=1,2,2,0"             height="200" width="200" align="center">            <param name="java_code"                        value="xptoolkit.applet.HelloWorldApplet">            <param name="java_archive" value="helloapplet.jar">            <param name="type" value="application/x-java-applet">            <comment>                <embed type="application/x-java-applet"                         codebase="http://java.sun.com/products/plugin/"                        height="200"                        width="200"                        align="center"                  java_code="xptoolkit.applet.HelloWorldApplet"                  java_archive="helloapplet.jar">            <noembed>            </comment>               <p> Java is cool. Get a browser that supports the plugin.                 </ br>              </p>            </noembed>            </embed>            </object> 

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"             height="200"             width="200"             align="center">     <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!         </p>     </jsp:fallback> </jsp:plugin> 

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

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 application in the JAR file. In order to do this, you must specify the name of the applications 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 chapter 5.

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.




Professional Java Tools for Extreme Programming
Professional Java Tools for Extreme Programming: Ant, XDoclet, JUnit, Cactus, and Maven (Programmer to Programmer)
ISBN: 0764556177
EAN: 2147483647
Year: 2003
Pages: 228

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net