Jump to Real's How-to Main page

Make a JAR executable

In the manifest file of a JAR, it is possible to specify the class to be used when the JVM is lauched with the JAR as parameter. The class must have a main().

Try with this simple class
import java.awt.*;
import java.awt.event.*;

public class MyClass {
 public static void main(String[] args) {
    Frame f = new Frame();
          (new WindowAdapter() {
              public void windowClosing(WindowEvent e) {
    f.add(new Label("Hello world"));
Then create this manifest file (manifest.mft) with any text editor.
Manifest-Version: 1.0
Main-Class: MyClass
Classpath: .\MyJar.jar
Next, you include the manifest file in the JAR (MyJar.jar) with the MyClass class.
jar cvfm MyJar.jar manifest.mft *.class
Then you are able to start the MyClass.class by double-clicking on the MyJar.jar file (if the JRE is correctly installed) or by typing
java -jar MyJar.jar
If you need to pass parameters, use the -D switch before de -jar switch and use the getProperty() method to get the value.

On NT, you can also make JARs run from the command-line by setting the PATHEXT environment variable, for example
Then if you have the jar file MyJar.jar accessible via the PATH environment variable, typing "MyJar" on the DOS command line will invoke "java -jar MyJar.jar" .

Written and compiled by Réal Gagnon ©1998-2000 mailto:real@rgagnon.com

[ home ]