The Personal Self-Assessment Environment
Creating executable jar files
Last modified: Fri Sep 3 09:55:32 HST 1999
A new and cool feature of Java2 is the ability to create executable jar
files. A jar file is simply a file containing a collection of java
files (typically, java .class files, but any other kind of file could
be included as well). To make a jar file executable, you need to
specify where the "main" Class is in the jar file, so the
"java" command knows what main() method to invoke to get the software
going. The procedure is quite simple, and this document provides
step-by-step instructions. For more details on jar files, consult
http://java.sun.com/docs/books/tutorial/jar/.
- First, make sure you have installed Java 1.2 or above. This
facility is not available in previous versions of Java.
- Next, create your working java system. In general, you will
want to put it into a package. For this example, I created a trivial HelloWorld
application that prints out "Hello World" plus the first command line
argument, and placed it into the package "psae". Therefore, the HelloWorld
files (HelloWorld.class, HelloWorld.java) were located in the directory
psae. I tested the system to make sure it worked before going on to the
next step.
- In the directory in which the psae is located, created a file
called "mainClass". This file contains a single line specifying where
the main Class is to be found in the jar file. Note that I use the package
specification. Here is the single line:
Main-Class: psae.HelloWorld
Note: make sure you type a carriage return after this line; some windows
systems need it and will report a "Failed to load Main-Class manifest
attribute" error.
- Next, I create a jar file called psae.jar using the "jar" command
in Java2. I use the "m" command line argument to specify the manifest
file mainClass, which adds information to the jar file on where the main
class will be found. Here is the jar command:
bertha:~ > jar cmf mainClass psae.jar psae
- Just for fun, and to check what's happened, I print the table of contents for the
jar file I just created. Here's the command and its result:
bertha:~ > jar tf psae.jar
META-INF/
META-INF/MANIFEST.MF
psae/
psae/HelloWorld.java
psae/HelloWorld.class
- Having successfully created the jar file, I can now invoke java2
on it with the command line argument:
bertha:~ > java -jar psae.jar Philip
Hello World Philip
- Don't believe me? Try it yourself with the actual jar file:
psae.jar.
Philip Johnson