Wednesday, October 19, 2005

Executable Jar File in JAVA.

Creating an executable jar file is not that tough if it does not involve any external jars. Create a manifest file and mention the main class path in the manifest file. Place all the class files in a folder along with the manifest file and run the following command:

jar -cfm [name of the jar file] [name of the manifest] *

Flags Description:
c: Create new archive
f: Specify the archive name
m: include manifest

* means include all the files in the current folder and all the sub folders.

The manifest file look something like this:

Main-Class: [name of the main class along with the package details]


Example
If your project has two files a.class and main.class. which are placed in a package(abc.def). Then the manifest would look like this(mainClass.mf):

___________________
Main-Class: abc.def.main
___________________

and the command to create an executable jar will be

jar -cfm test.jar mainClass.mf *

Everything works ok if you are not using any external jars but the problem arises when you have externals jar like you have used the logging component log4j. The manifest attribute Class-Path attribute in which you can mention the paths of the external jars, but i tried it for log4j but it didn't work it gives me error 'Logger Class Def Not Found'. I had created the following manifest for that (Keeping the above exapmle in mind)

__________________
Main-Class: abc.def.main
Class-Path: lib\log4j.jar
__________________

but this didn't worked.

Then i came across a work around for this, which is you create a jar file of your project class files without adding the extenal libaries ie. using the following same manifest:

___________________
Main-Class: abc.def.main
___________________

Place the created jar files along with the external jar files in another folder and then use the following manifest to create the final jar file.

____________________
Main-Class: abc.def.main
Class-Path: main\test.jar lib\log4j.jar
____________________

Notice that test.jar is the jar file of your actual project. the final jar created using the above manifest will work for you.

6 comments:

Anonymous said...

Nice site!
[url=http://vjdrduqb.com/qwee/ovpx.html]My homepage[/url] | [url=http://qexuzvqh.com/dgea/scac.html]Cool site[/url]

Anonymous said...

Well done!
My homepage | Please visit

Anonymous said...

Thank you!
http://vjdrduqb.com/qwee/ovpx.html | http://ylgfiqih.com/nywe/fkmg.html

dilip gupta said...

excellent blog...
now i am able to solve my issue.
one more thing i want to ask,
we are using one license file also with the executable file. How can i do this? How can i integrate license file with my executable file.

Zahid said...

Escellent.... thank you for sharing this information

Smita said...

Thanks a ton I can use this technique for the junit tests as well