JAVA Creating an Executable Jar File

A Java Archive file (Jar) is a compressed file based on the ZIP format.  Jar files can contain many files and folders, but most often hold Java .class files.  This format helps keep all the files in one place, and reduce the overall size of a project.  Another very useful function of Jar files is that some of them can be executed.  The purpose of this tutorial is to explain how to create an executable Jar file.

First I’ll make the assumption that you have downloaded and installed the Java SE Development Kit (JDK), and have also set up the PATH and CLASSPATH environment variables.  If you can run and compile .java programs from the command line, then you’re set to go.

First thing you need is a Java source file.  A simple program that just gets and sends data to the command prompt will work as long as you run it from the command-line, but you will not be able to see any output if you just double click on the Jar file.  So, in this tutorial I will use an example that creates a window using JFrame.

Step 1.  Create a folder called test and put your Java source file in it.  For my example I’ve created a simple clock application that just counts the number of days since something has happened…the number of days since you’ve quit a bad habit, the number of days your website has been up, for me it’s the number of days since I joined Code Call…13 days at the writing of this tutorial.

Posted Image

You can use the example if you like or substitute one of your own.  Go ahead and compile the source.  You should now have a test folder that contains the source file, and two class files.

Posted Image

Step 2.  Create a manifest file…I’ll call mine Clock.MF.  A manifest is basically a file that contains information about the files packaged inside the Jar file.  You can create yours using a text editor, just give it a .MF extension. Here’s what needs to go in this manifest file:

Manifest-Version: 1.0
Main-Class: Clock
Created-By: 1.2 (Sun Microsystems Inc.)

The first line tells us that this file conforms to 1.0 of the manifest specification.  The second line tells which class inside the Jar file contains the “main” function.  And the last line defines the version and the vendor of the java implementation on top of which this manifest file is generated.

Step 3.  Create the Jar file using the jar command.  In the example below (jar) is the name of the executable you call to create the Jar file.  The jar command has a number of options…I used four of them (c –create the jar file, v –generates verbose output to the screen during file creation, f –specifies the file to be created, and m –indicates the manifest file to be used)  After the options parameter comes the name of the jar file to be created (clock.jar), followed by the name of the manifest file to use (Clock.MF).  The (*) wild card indicates to include all files in that directory.  If you wanted you could use *.class to only include class files.  Here's the command:

jar cvfm Clock.jar Clock.MF *

Your  test folder should now contain five files…one of them being the new clock.jar file.  You should be able to double click on the clock.jar, and it will execute.  You can also exec
ute this code from the command line by typing (java -jar clock.jar).

Posted Image

Read more:

No comments:

Post a Comment