Sunday, 26 August 2012

Making of executable jar by using "jar" utility in jdk

Hi,

We can make the jars and executable jars by using the  "jar" utility present in the jdk as under:

1. Create the class having the main() method in package say main.pckage.MainClass.java
2. Create another class having any method in package say com.other.pckage.OtherClass.java.
3. Compile both classes.
4. Create a text file say manifest.txt and write the Main-Class: main.pckage.MainClass
5. Now we have two main folder main and com containing the .class files and one manifest.txt file.
6. Put all these in one folder say "Executable Jar".
7. I assume that you set the java path variable.
8. Open the command prompt and paste the
cmd-->  C:\Users\Rajan Bansal\Desktop\Executable Jar>jar cvfm myjar.jar manifest.txt com
/other/pckage/*.class main/pckage/*.class
 and the output is:
cmd--> added manifest
adding: com/other/pckage/OtherClass.class(in = 547) (out= 344)(deflated 37%)
adding: main/pckage/MainClass.class(in = 754) (out= 454)(deflated 39%)

Now we have successfully created the executable jar file in cmd.

Now write cmd--> java -jar myjar.jar
It will execute the main method of MainClass.java

And we can use that jar in any of our projects.

For more info click here..

No comments:

Post a Comment