Add to Google Reader or Homepage

Creating Executable JAR

Executable JAR's

Executable JAR's are Java Archive Files(JAR) that contains an application(along with its resources) which can be executed by using either java program launcher or in case of windows by simply double-clicking the jar file.

One of the main-difference between an ordinary jar file and an executable jar file is that it has an attribute named "Main-Class" associated with it in the manifest file.

The Main-Class attribute is used to specify the entry point in the jar file. 

"The entry point denotes the class that you want to execute when a use double-clicks a jar file or uses java launcher to execute the file".

The entry point must denote the main class i.e, a class that contains public static void main(String args[]). 

There are two possible ways to create an executable JAR.

1.When creating a jar file use the option e to create an entry point in the Manifest.

See the following sample to understand the above method of creating an executable jar file.



C:\>jar cvfe Add1.jar Add Add.class
added manifest
adding: Add.class(in = 1450) (out= 872)(deflated 39%)

In this example,

Add1.jar-  name of the JAR file.

Add-   it is the entry point in the manifest i.e the class that is to be executed (Main class)

Add.class-The resource file that i have included in the JAR.

If you see the manifest of the generated JAR then you can identify a Main-Class attribute as follows.

Manifest-Version: 1.0
Created-By: 1.6.0 (Sun Microsystems Inc.)
Main-Class: Add

2. The Second method for generating a executable jar would be quite difficult compared to the first method.

Here you have to create your own Manifest file as follows:

2.1.Open the NotePad and type the attributes as i have shown below.

Manifest-Version: 1.0

Main-Class: Add



 There are certain things which i feel quite worth to be mentioned here when creating a manifest file.

1.When writing the attributes ensure that one line space is available between them.Simply press the Enter key double time immediately after writing an attribute.

2.If you do not leave one line space or leave some extra space between the attributes then  the desired manifest file would not be generated.

2.2  After finished writing the attributes save the file with .mf extension.

Now we can create the executable jar file using the following command line.

C:\>jar cvfm Add.jar Add.class
added manifest
adding: Add.class(in = 1450) (out= 872)(deflated 39%)

Add.jar- name of the jar file. manifest file that i have created by using the notepad.
Add.class- resource file that i have added into the jar file.

Remember one thing,in windows executing a jar using a launcher or by double-clicking it will not generate a shell window.

java errors and exceptions © 2010 | Designed by Chica Blogger | Back to top