Sunday, March 1, 2009

Executable Jar file

Hi friends..Island with a palm tree

Here is a process that we will create an executable jar file so when you double click on this jar file it execute (run).

There are a lot of method that i am describing here.......
First method using Eclipse-----------------
(Easy and best, but you can clear all concepts using this process) ----
So, today it is very easy to create an executable Jar file with Eclipse 3.x classic version.

Create a Java program name it as "Java Example"and obvious there will be a main program and other related files.

Right click on the "Java Example" select Export option and the type Jar, u will see more option and select Executable jar file there.

Click next and select two options from up side for you program name and where to save your jar file and also give a name for this jar file.

Then u click next and then next.

U will find a jar file that will be executable and if you double click on it and it will run without any error.

Second method ----------------------------Using the command prompt.



Step 1: From command prompt cd to the directory where your class files present

Step 2: execute the below command for JAR creation

> jar cmf temp.jat jarcreator

Here:

temp.jar >> Temporary JAR file name.

jarcreator >> Class file package name.

Step 3: This will generate the jar named temp.jar. Now extract the temp.jar you will see one META-INF folder, inside that

You can see there is MANIFEST.MF file. Copy this file outside the folder and keep in parallel to your top package.

Like I keep it like this in my project named KeyHandler :

KeyHandler

bin

jarcreator

MANIFEST.MF

src

jarcreator

.classpath

.project

Step 4: Now delete the extraacted folder and the temp.jar file (This temp.jar we had created to get the MANIFEST file templete).

Step 5: Manifest file modification.

Open your MANIFEST.MF file you will see something like below ( May be you having diffrent JDK version information because I have JDK 1.6 installed)

At the end of file add below line: Main-Class: jarcreator.KeyTester

Here: KeyTester is the class file in which main method is present.


EXAMPLE:

Manifest-Version: 1.0

Created-By: 1.6.0_06 (Sun Microsystems Inc.)

Main-Class: jarcreator.KeyTester

Note: After adding the Main-Class tag in manifest file press enter ( For new line ) then save the file otherwise it will not work.

Step 6: To make the executable JAR use below command:

jar cmf MANIFEST.MF myExecutable.jar jarcreator

Here:

MANIFEST.MF >> Your modified Manifest file.

myExecutable.jar >> Name of the jar file. Keep according to whatever JAR name you want

jarcreator >> Top level package name.

Step 7: Now just double click on the myExecutable.jar it will open the applet.



Thanks & Regards
Ajay Singh
Member- Technical
Shigan Telematice Pvt. Ltd. Gurgaon