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
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.
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:
public class Dummy
Step 2:
Main-Class: Dummy
NOTE: You need to make sure there is a new line after the Dummy Step 3:
jar cmf mymanifest.mf mydummy.jar Dummy.class NOTE: Assuming your Dummy.class and mymanifest.mf files are in the same directory. Step 4:
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:
NOTE: Please make sure that you have an empty line after
the Main-Class entry in your manifest file. Otherwise, it will NOT work!!!!
Do you have a Java Problem?
Java Books
Return to : Java Programming Hints and Tips All the site contents are Copyright © www.erpgreat.com
and the content authors. All rights reserved.
|