Wednesday, September 28, 2011

Create Executable jar

The steps to create Executable jar are as follows,

1) create a java class under the package test.

Executablejar.java
---------------------

package test;
class Executablejar
{
public static void main(String a[])
{
System.out.println("HELLO WORLD");
}

}

2)compile above java class

C:\>javac Executablejar.java -d .

here don't forget to give space between  -d and dot(.) 

3)once run the Executablejar class
to run use following command,
c:\>java test.Executablejar

then you can see "HELLO WORLD" is displayed on console.

4)Then create this manifest file (manifest.txt) with any text editor.

manifest.txt
------------------
Main-Class: test.Executablejar

Note:Don't forget to press enter after write above line and then save.otherwise you get an error
message as "Failed to load Main-Class manifest attribute from
myjar.jar"

5)Next, preparation of executable jar
 
c:\>jar cvfm myjar.jar manifest.txt test
 
here test is the package name of my class .
 
6)Then you are able to start the Executablejar.class by double-clicking on the myjar.jar file (if the JRE is correctly installed) or by typing

c:\>java -jar myjar.jar
 
then you get output as "HELLO WORLD"
 
 
 
Note:
To view the content of the jar file you can use the fallowing command
C:\>jar tf myjar.jar
It displays as:
D:\javaprogramess >jar tf myjar.jar
META-INF/
META-INF/MANIFEST.MF
test/
test/Executablejar.class
 



No comments:

Post a Comment