JARs


When you learned about the Java classpath in Lesson 1, you learned to add class folders (or directories) to your classpath. A class folder contains discrete class files that Java looks for when it needs to load a class.

Your classpath can include JARs in addition to, or instead of, class folders. A JAR (Java ARchive) is a single file that collects Java class files and other resources needed at runtime. You can create JARs using the jar command-line utility executable supplied in your Java bin directory. You can also create JARs using Ant.

An understanding of JARs is absolutely essential to being able to work in any production Java environment. The usage of JARs falls under the category of deployment, so the discussion of JARs didn't have an appropriate place earlier in the book.

The jar utility combines class files and other resources (such as images or property files) into a single file with an extension of .jar. The utility creates JARs as ZIP files, a file format you are likely familiar with. A ZIP file uses a standard compression algorithm to reduce the overall space requirements.

There are many reasons to use JARs:

  • Deployment speed. Transferring or installing a few files is considerably faster than transferring or installing thousands of files.

  • Compression. Disk requirements are smaller; smaller files are downloaded faster.

  • Security. When working in secure environments, you can attach a digital signature to a JAR.

  • Pluggability. You can organize JARs around reuse interests. JARs can be components or complete API sets.

  • Versioning. You can attach vendor and version information to JARs.

  • Simplified execution. You can designate a JAR to be "Java executable." A user of the JAR does not need to know an entry class name.

JARs are the basis for deploying classes to a web server or enterprise Java installation.

To create a JAR for the SIS application, first navigate to the classes directory. Then execute:

 jar cvf sis.jar * 

In the above jar command, the options cvf (c, v, and f) tell the JAR command to create a new JAR, to provide verbose output (not necessary but useful), and to use a filename of sis.jar. The * at the end of the command tells the jar program to archive all files in the current directory and all subdirectories. You can execute the command jar with no arguments to display a brief summary of the command and its proper usage.

After executing the jar cvf command, you should have a file named sis.jar. Under Windows, you can open this JAR using WinZip. Under any platform, you can use the jar command to list the contents of the JAR.

 jar tvf sis.jar 

The only difference in the command is the option t (list table of contents) instead of c. You should see output similar to this:

    0 Mon Aug 02 23:25:36 MDT 2004 META-INF/   74 Mon Aug 02 23:25:36 MDT 2004 META-INF/MANIFEST.MF  553 Sat Jul 24 10:41:28 MDT 2004 A$B.class  541 Sat Jul 24 10:41:28 MDT 2004 A.class 1377 Sat Jul 24 10:41:28 MDT 2004 AtomicLong.class    0 Sat Jul 24 10:41:28 MDT 2004 com/    0 Sat Jul 24 10:41:28 MDT 2004 com/jimbob/    0 Sat Jul 24 10:41:28 MDT 2004 com/jimbob/ach/  486 Sat Jul 24 10:41:28 MDT 2004 com/jimbob/ach/Ach.class  377 Sat Jul 24 10:41:28 MDT 2004 com/jimbob/ach/AchCredentials.class  516 Sat Jul 24 10:41:28 MDT 2004 com/jimbob/ach/AchResponse.class 1164 Sat Jul 24 10:41:28 MDT 2004 com/jimbob/ach/AchStatus.class  ... 

You can extract files from the JAR using the command option x (i.e., jar xvf).

Once you have added classes to a JAR, you can add the JAR to your classpath. Java will dig into the JAR file to look for classes it needs to load. For example, suppose you deploy sis.jar to the directory /usr/sis. You can execute the SIS application using the command:

 java -cp /usr/sis/sis.jar sis.ui.Sis 

In order for Java to find a class within a JAR, its path information must match the package information. To locate the class com.jimbob.ach.Ach, Java must be able to find an entry in the JAR with the complete pathname com/jimbob/ach/Ach.class. In the jar tvf listing, I've shown this entry in bold.

If you were a directory higher when you were creating the JAR, the path information in the JAR file might be:

    486 Sat Jul 24 10:41:28 MDT 2004 sis/com/jimbob/ach/Ach.class 

Java would not match up this entry with the class com.jimbob.ach.Ach.

The jar utility adds a manifest file, META-INF/MANIFEST.MF, to the ZIP file. The manifest file contains information about the contents of the JAR (i.e., meta-information).

One use for the manifest file is to indicate a "main" class. If you specify a main class, you can initiate an application by providing only the JAR name. You can accomplish this by first creating a separate manifest file with the contents:

 Main-Class: sis.ui.Sis 

Make sure you include a blank line as the last line of this file! Otherwise Java may not recognize the manifest entry.

When you create the JAR, specify the manifest file using the command:

 jar cvmf main.mf sis.jar * 

The m option stands for manifest, of course. If you look at the file MANIFEST.MF in sis.jar, it should look something like this:

 Manifest-Version: 1.0 Created-By: 1.5.0 (Sun Microsystems Inc.) Main-Class: sis.ui.Sis 

You can now initiate the SIS application using the simplified command:

 java -jar sis.jar 

You can manipulate JARs (and ZIP files) programmatically. Java supplies a complete API in the package java.util.zip to help you accomplish this. The java.util.zip package contains tools to both read and write JARs. This allows you to write unit tests against JAR operations about as simply as if you were testing against text files.



Agile Java. Crafting Code with Test-Driven Development
Agile Javaв„ў: Crafting Code with Test-Driven Development
ISBN: 0131482394
EAN: 2147483647
Year: 2003
Pages: 391
Authors: Jeff Langr

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