Runnable JAR archives

You can run a program stored in the JAR archive that has a main() method like this:

$ java -cp eharold.jar MainClassName

You must use the fully package qualified name. For example,

$ java -cp eharold.jar

The -cp flag adds the jar file to the class path.

In Java 1.2 you can add a Main-Class attribute to a JAR file's manifest so that the person who runs the program does not need to know the name of the name of the class with the main() method. This attribute has the following form


Put this line into a file called (for example) MyManifest.txt. Then use this command line to package the JAR:

$ jar cvmf MyManifest.txt eharold.jar edu

This will copy data from the file MyManifest.txt into the JAR's own manifest file, and add the directory edu to the archive.

To run the program packaged in the jar file eharold.jar you simply type:

$ java -jar eharold.jar

Java will look inside the JAR archive's manifest to find which class's main() method it should run.

Previous | Next | Top | Cafe au Lait

Copyright 1999 Elliotte Rusty Harold
Last Modified June 17, 1999