All JAR files contain something called a manifest file which holds information Java wants to know. One piece of information a manifest file may contain is the name of a class that will be run if the JAR file is executed.

The first thing you must do is create a text file that lists the "main" class - the class that has the main method you want executed when the JAR is executed. Let's say that Three from the above example has the main method I want executed. I create a text file called "mainClass.txt" with the following text:

Main-Class: Three

IMPORTANT: the text file only needs the one line of text for this purpose. However, the file must end with a blank line or this will not work, ie the file has two lines in it - the second one is empty. Note too the class is called "Three" and not "Three.java" (the file containing the source code) or "Three.class" (the file containing the byte codes). If your class file is in a package hierarchy, you must use the fully qualified name of the class (eg "myPackage.MyClass").

I then run the jar utility with this command line:

jar cmf mainClass.txt example.jar *.class

With this line, I told jar to create a JAR file (option c) with modifications to the manifest file (option m) as specified within mainClass.txt, naming the JAR file (option f) as example.jar and including everything that matches the pattern *Class.