How to create an executable JAR file?

Following example would show you how to create and execute an executable JAR file.

1. Create a JAVA application and compile it. For instance, I have taken a simple example of a Hello World program below:

package helloworld;

public class HelloWorld {

   public static void main(String[] args) {

        System.out.println("Hello World!");

2. Create a text file named Manifest.txt with the following contents

Main-Class: helloworld.HelloWorld

[Please note that the Manifest.txt file must end with a new line or a carriage return. This convention is a mandatory action to follow otherwise it would not work. This action is refered as setting an entry point of the Java application in the Manifest file – which means you tell JAR file that while executing the JAR, execute the main method of the class you mentioned in the Manifest file.]

3. Create a JAR file with a name – HelloWorld.jar using the below command

jar cfm HelloWorld.jar Manifest.txt helloworld/*.class

You are done. You have now created an executable JAR file.

How to run an executable JAR:

java -jar HelloWorld.jar

[The above command will execute the HelloWorld JAR file by executing the main method of class]