Create Executable Jar Files

How to create executable jar?
 
You can make your Java application runnable via a double-click by packaging it into a so called executable jar. You do that by specifying the main class of your application, any extra jar files it may require and so on in the jar's manifest file.
 
Main-Class: MyAppMain
Class-Path: mylib.jar
 
Then you use the jar utility from the Java SDK to package your classes and resource files, specifying the m option and the name of your manifest file: 
 
jar cvfm MyApp.jar MyApp.mf *.class *.gif
 
This will result in the creation of MyApp.jar. Now, if you type 
 
java -jar MyApp.jar
 
the Java launcher will read the manifest from MyApp.jar and invoke the main method from the class MyAppMain. Moreover, if you double-click that jar file on a system that has JRE installed, the java launcher will be invoked automatically. 
 
Note: As of J2SE 5.0, jar files are associated with the javaw launcher on Windows, which does not open a console on startup. If your application needs a console, write a batch file which would start it using the java launcher. 
 
If your application consists of more than one jar file, there is an open source tool called One-JAR that claims to correctly repackage multiple jars into one. 
 
The major problem with executable jars is compatibility. The default JRE may be of an older version than is required by your application or may not have the necessary Java Optional Packages (previously known as Standard Extensions) installed. For instance, if your app uses the java.nio package introduced in Java 2 version 1.4, it will not work on JRE 1.3.x. Similarly, if it uses JavaMail 1.3, and the default JRE has JavaMail 1.2 or JavaMail is not present at all, the double-clicked jar will not run. 
 
Fortunately, Sun has created a Java application deployment technology that eliminates this compatibility problem and adds some nice features. It is part of the Java 2 platform since version 1.4 and is called.
 

Question:
 
I know that there is a way to create jar files so that when you double click them they run like a normal program. I also know that to do this you need to add the Main-class: classname line to the jars manifest file. However, every time I do create a jar file with this line in its manifest file, I keep getting the following error.
Failed to load Main-Class manifest attribute from C:\j2sdk1.4.2\bin\risk.jar
Can anyone help me resolve this? More specifically, I'm kinda confused about using the jar program that comes with the j2sdk. any pointers on that?
 
Answer:
 
Make sure your manifest file contains the new line (cr-lf) after the Main-Class entry. Also, the classname must contain the signature of main function( public static void main(String[] args) ) and if it has a package name then you sould need to include the package name.
 
Here is an example of how would package your application as a jar file:
 
Step 1:
-------
Create a java application, in this case it is just a Dumy class prints out the "Hello, World" message:
 
public class Dummy
{
public static void main( String[] args )
{
System.out.println( "Hello, World" );
}
}
 
 
Step 2:
-------
Create a manifest file contains the Main-Class attribute. Your manifest file should look like the following:
 
Main-Class: Dummy
 
 
NOTE: You need to make sure there is a new line after the Dummy
 
Step 3:
-------
This step assume that you have compiled your source code into byte-code, and also your manifiest file name that you have created in step 2 is "mymanifest.mf".
Creating the jar file by using jar command as follows:
 
jar cmf mymanifest.mf mydummy.jar Dummy.class
 
NOTE: Assuming your Dummy.class and mymanifest.mf files are in the same directory.
 
Step 4:
-------
To execute it just type as follows:
 
java -jar mydummy.jar
 
It should print out the "Hello, World" message.
 
Comment:
 
Take note that you cannot have a .class extension in the Main-Class entry.
 
1. the value of Main-class entry should contain the class name (including the package name that it belongs to) without the .class extension.
 
Suppose you have a package name games.risk under C:\MyGame directory, and you want to make a jar file with the manifest file, let's say its name is mymanifest.mf and located in C:\MyGame directory, with bunch of classes under package name games.risk.
 
Here is the command you can use to accomplish that:
C:\MyGame>jar cmf mymanifest.mf games.jar games
 
NOTE: Please make sure that you have an empty line after the Main-Class entry in your manifest file. Otherwise, it will NOT work!!!!
and also the classname you specify in the Main-Class contain the main method.

Do you have a Java Problem?
Ask It in The Java Forum

Java Books
Java Certification, Programming, JavaBean and Object Oriented Reference Books

Return to : Java Programming Hints and Tips

All the site contents are Copyright © www.erpgreat.com and the content authors. All rights reserved.
All product names are trademarks of their respective companies.
The site www.erpgreat.com is not affiliated with or endorsed by any company listed at this site.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
 The content on this site may not be reproduced or redistributed without the express written permission of
www.erpgreat.com or the content authors.