Sunday, December 24, 2006

JAR Files

JAR file (or Java ARchive) is a ZIP file used to distribute a set of Java classes. It is used to store compiled Java classes and associated metadata that can constitute a program.
__________________________________________________________
How to Create JAR file :

*Traditional method:

*Quick Method:

1- put all your .class files and packages next to a folder named "META-INF" containing a file named "MANIFEST.MF"
2- open "MANIFEST.MF" using any text editor
3- write in "MANIFEST.MF" file
Manifest-Version: 1.0
Main-Class: MainClass
where MainClass is your main class
4- add your .class files and this folder to a ZIP archive ( not .RAR ) using WinRAR or any other program
5-change the extension of resulting file to .jar

*Preferred Method for me:
Generate it using IDE you use
In Eclipse 3.2, follow
File -> Export -> Java -> Jar File
and continue with the wizard specifying your main class and exported classes
_________________________________________________________________________
How to run a jar file:

*In case you have JDK 1.5 Updat 8 or above:
double click on JAR file or use the fllowing method.

*In case of older versions of JDK :
1- create a batch file ( you can create a text file and change its extension to .bat)
2- wirte in batch file "java -jar file.jar" to execute file.jar archive
3- double click on batch file

For more about JAR files, check