In this tutorial, we show you how to create a JAR and make it executable by double clicks on it.

1. AWT Example

Create a simple AWT Java application, just display label and print out some funny characters ~

package com.mkyong.awt;
 
import java.awt.Frame;
import java.awt.Label;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
 
public class AwtExample {
 
	public static void main(String[] args) {
 
	    Frame f = new Frame();
	    f.addWindowListener
	          (new WindowAdapter() {
	              public void windowClosing(WindowEvent e) {
	                 System.exit(0);
	                 }
	              }
	    );  
	    f.add(new Label("This JAR file is executable!"));
	    f.setSize(500,500);
	    f.setVisible(true);
	}
}

2. Manifest.txt

Create a manifest.txt text file. Main-Class defines the entry point of this Jar file, when you double clicks on this Jar file, the “AwtExample.class” main() method will be launched.

File : Manifest.txt – Only one statement and end with a new line character (double clicks at the end of the statement to create a new line)

Main-Class: com.mkyong.awt.AwtExample
Note
Be sure that your manifest file ends with a new line, else your manifest file content will not be parsed and append to the system generated manifest.mf.

You can read this manifest.mf reference guide, and look for the following statement.

Be sure that any pre-existing manifest file that you use ends with a new line. The last line of a manifest file will not be parsed if it doesn’t end with a new line character.

3. Jar file

Create a Jar file by adding “AwtExample.class” and “manifest.txt” files together.

Assume your project folder structure as following :

c:\test\classes\com\mkyong\awt\AwtExample.class
c:\test\classes\manifest.txt

You can issue this command to create a “AwtExample.jar” file.

jar -cvfm AwtExample.jar manifest.txt com/mkyong/awt/*.class

Output

C:\test\classes>jar -cvfm AwtExample.jar manifest.txt com/mkyong/awt/*.class
added manifest
adding: com/mkyong/awt/AwtExample$1.class(in = 638) (out= 388)(deflated 39%)
adding: com/mkyong/awt/AwtExample.class(in = 880) (out= 541)(deflated 38%)

4. Demo

Now, the “AwtExample.jar” is executable, double clicks on it, see output :

make-jar-executable