Here is the general procedure for creating an executable JAR:
- Compile your java code, generating all of the program’s class files.
- Create a manifest file containing the following 2 lines:
Manifest-Version: 1.0 Main-Class: name of class containing main
The name of the file should end with the .mf suffix but it is not compulsory. It is important that the file ends with a blank line.
- To create the JAR, type the following command:
jar cmf manifest-file jar-file input-files
The input-files must include any class files, images, sounds, etc. that your program uses. Optionally, you can include the program’s
.javafiles in the JAR.
- To view the contents of the JAR, type:
jar tf jar-file
- Execute the application from the command line by typing:
java -jar jar-file
If the application is GUI-based, you can also launch it by double-clicking the JAR file.
A simple example.
Let’s say we wanted to distribute the simple program Hello.java as a JAR.
First, we create a text file named Hello.mf which contains:
Manifest-Version: 1.0 Main-Class: Hello
Then, we create the archive by typing:
jar cmf Hello.mf Hello.jar Hello.class Hello.java
and run it by typing:
java -jar Hello.jar
Manohar Bhattarai (मनोहर भट्टराई)
Facebook : http://www.facebook.com/pages/Manohar-Bhattarai/130973796914160
Twitter : http://twitter.com/mhrbhattarai
Identica : http://identi.ca/manoharbhattarai