Saturday, August 15, 2009

Java: How to create a jar archive


Share at Facebook

I don't used to make jar file to often to execute my program. I used to make a bat file, and paste my java xyz.class command inside that batch file and run. What I do when I need to make a jar file? I just do the below steps when I need.

Lets say your main class name is ICFun
1) First of create a Manifest file called ICFun.MF, and inside the file paste below lines


Manifest-Version: 1.0
Main-Class: ICFun
Created-By: 1.2 (Sun Microsystems Inc.)


2) Create a batch file, which will import the path of your jar file at first line, and second line will contain the jar creating command with required classes.

path="C:\Java\jdk1.6.0\bin"
jar -cvfm ICFun.jar ICFun.MF *.class


You can add the class using folder, for example, folder1/*.class folder2/classes etc.

3) Now your ICFun.jar file is ready. You can run it by clicking it, or from command line.
java -jar ICFun.jar

That's what I do for my java archive creating from my class files.




2 comments:

Anonymous said...

Using this procedure, I'll end-up with three files: ICFun.java, ICFun.MF and ICFun.bat(?) .
How do I merge them into single file ICFun.jar?

Wolf said...

Just double clicking on the ICFun.bat file will create your ICFun.jar file. If the jar file creation failed, then you should correct the java path inside your .bat file.

I think i missed this part on this article. Thank you for noticing this.