2. Compress Files To An Executable Java Archive (JAR):
It is also possible to make an archive that can be executed (run) by Java and even by a double click through your OS, if you have it set up correctly.
Jar file contains MANIFEST file that contains information Java wants to know.
One piece of information a manifest file may contain is the name of a class that will be run if the JAR file is executed.
To create an executable jar file you must specify in which file the main method is there.
You do this by creating a text file called "mainClass.txt" with the following text:
Main-Class: Two
Where Two is the java file name which contains the main method.
IMPORTANT: the text file only needs the one line of text for this purpose. However, the file must end with a blank line other wise this will not work.
Now run the jar utility with this command line:
jar cmf mainClass.txt example.jar *.class
observe m option and mainClass.txt you will get cleared.
i.e.
create a JAR file (option c) with modifications to the manifest file (option m) as specified within mainClass.txt, naming the JAR file (option f) as example.jar and including everything that matches the pattern *Class.
Running An Executable JAR From Command Line
java -jar example.jar
In windows you can just double click on a jar file instead of doing all these typing.
For this you should have the following to be set.(It is automatically done with java installation but if not you need to do it manually).
‘javaw.exe’ is an application comes with JDK for allow you for Double-click.
Go to Tools | Folder Options | File Types.
note: see the above image
"C:\Program Files\Java\jre6\bin\javaw.exe" -jar "%1" %*
IMPORTANT NOTE: include the double quotes to take care of names with spaces.