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]

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(

Main-Class: abc.def.main

and the command to create an executable jar will be

jar -cfm test.jar *

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.


Anonymous said...

Hey, you have an impressive blog here! I'm surely going to bookmark you! I have a Heaters site. It pretty much covers Heaters related stuff.

Come and check it out when you get time :-)

Anonymous said...

I enjoyed reading some of your posts. I have a best home based businesses site. You'll find all the information you need about starting your own best home based businesses. There are also great businesses for stay at home moms. Come and check it out if you get time :-)

Anonymous said...

Nice site!
[url=]My homepage[/url] | [url=]Cool site[/url]

Anonymous said...

Well done!
My homepage | Please visit

Anonymous said...

Thank you! |

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

MONY said...

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