< Zurück | Inhalt | Weiter >

5.11.1 Deploying Applications

One of the best uses of jar is to package applications for distribution. You can put a large Java application into a single file with jar, and by using a manifest (which we are about to discuss) you can nominate the main class to run in that JAR file. You can then provide a shell script (and a batch file, if you are also deploying to Microsoft Windows) that will set the CLASSPATH to point to the

JAR file and run java against it. With this simple setup, users need not even know they are using a Java application—it runs like any other application. The Manifest File

The only way in which jar really differs from any other ZIP archive utility is in the automatic creation and use of a manifest file, by default named META-INF/MANIFEST in the archive. Even if you do not specify a manifest file of your own, the jar utility creates one for you. Let’s take a moment to look at what goes into the manifest.

A manifest is basically a list of key/value pairs. The key comes at the start of a line and the value comes at the end of the line with a colon separating the two. Example 5.21 shows a sample manifest.


Example 5.21 Manifest from the Payback sample application

Manifest-Version: 1.0

Ant-Version: Apache Ant 1.5.3

Created-By: 1.4.1_02-b06 (Sun Microsystems Inc.) Version: 1.0

Main-Class: net.multitool.Payback.Payback


All of these entries were produced automatically by ant or the jar utility itself, except for Main-Class, which we specified (albeit with ant, as you will see in Chapter 9). The manifest has certain values that are always filled in by jar, but two that you might commonly specify are

Main-Class, which allows users to run a Java application by merely typ- ing java someJarFile.jar, without having to know the fully package qualified name of the class that contains the application’s main().

Class-Path, which allows you to specify what the classpath should be when the application is run.

There are keys specific to applets, to signed applications, to beans, and so forth. We will address these as it becomes necessary. Full details can, of course, be found in the Sun’s documentation for jar.27


27. http://java.sun.com/j2se/1.4.2/docs/tooldocs/solaris/jar.php Putting a Compiled Application in a JAR File

Let’s assume we are going to manually put a Java application in a JAR file. We will want to specify the name of the class that contains the main() method of the application. First off, you want the JAR’s directory hierarchy to begin at the folder that contains the first node of each package’s name. Our sample ap- plication here is in the package net.multitool.Payback, so we want our present working directory to be the one which contains the net subdirectory. Here’s a dump of the directory tree from that point after compilation of our sample application:

$ find . -print

















We now want to specify which class contains the application’s main() method. It happens to be the Payback class, so we create a file called manifest28 with the following contents:

$ cat manifest

Main-Class: net.multitool.Payback.Payback

Next, we use the jar utility to create the JAR file:


28. It can have any name. The key/value pairs from the file will be placed by the jar utility into the standard manifest called META-INF/MANIFEST.MF no matter what name you give to this file.

$ jar cmf manifest payback.jar net

$ ls -la total 20

drwxrwxr-x 3 mschwarz mschwarz 4096 Aug 4 18:19 .

drwxrwxr-x 7 mschwarz mschwarz 4096 Aug 4 17:57 ..

-rw-rw-r-- 1 mschwarz mschwarz 43 Aug 4 18:17 manifest

drwxrwxr-x 3 mschwarz mschwarz 4096 Jul 28 16:16 net

-rw-rw-r-- 1 mschwarz mschwarz 7506 Aug 4 18:21 payback.jar

The options to jar tell it what to do. In our case, -c instructs to create a JAR file, -m adds the contents of the file named in the next parameter to the META-INF/MANIFEST file, -f and the next parameter is the filename of the JAR file being created. If we had not specified -f, the JAR file would have been written to standard out and an I/O redirect would be needed, but the result would have been the same:

$ jar cvm manifest net > payback.jar

$ ls -la total 24

drwxrwxr-x 3 mschwarz mschwarz 4096 Aug 4 18:24 .

drwxrwxr-x 7 mschwarz mschwarz 4096 Aug 4 17:57 ..

-rw-rw-r-- 1 mschwarz mschwarz 43 Aug 4 18:17 manifest

drwxrwxr-x 3 mschwarz mschwarz 4096 Jul 28 16:16 net

-rw-rw-r-- 1 mschwarz mschwarz 7506 Aug 4 18:27 payback.jar

Everything that follows parameters required by option letters is considered to be a file or directory that is to be added to the JAR file. The option syntax for jar is similar to that for pkzip in the DOS/Windows world and the tar utility in the UNIX world.

As elsewhere in this chapter, we are just getting you started. See Sun’s documentation for details.