tags:

views:

729

answers:

6

Hi I have created a java application which depends upon some external jars. Now I want to create an executable jar for my project. Means if i double click the project then it should execute.

Thanks Sunil Kumar Sahoo

A: 

Look at using a packaging tool such as IzPack or a wrapper tool such as JSmooth

Rich Seller
+1  A: 

You will need to add a MANIFEST.MF file to the JAR for your application, (under the META-INF directory - if you use the 'jar' command line tool it will make sure the file ends up in the right place). It will need to contain two attributes (at least):

  • Main-Class: [the fully qualified name of the class in your app that has a main method]
  • Class-Path: [the list of JAR dependencies for your application]

More details on manifest files in JAR files can be found here: http://java.sun.com/docs/books/tutorial/deployment/jar/manifestindex.html

If you're using a build tool like Apache Maven you might find that it is able to generate this for you.

Martin McNulty
A: 

If you use Maven the assembly plugin will do this for you very simply: http://maven.apache.org/plugins/maven-assembly-plugin/howto.html

Otherwise you'll need to follow the instructions in the JAR file tutorial: http://java.sun.com/docs/books/tutorial/deployment/jar/index.html and creating a manifest file including your main class as Main-Class: [classname] and listing your external jars as Class-Path: theirJar1 theirJar2 etc.

Jim Downing
+1  A: 

You want to set the Class-Path attribute in your JAR's Manifest file.

This page should give you a good starting point.

developmentalinsanity
+2  A: 

You can do that easily with Ant:

<jar jarfile="MyJar.jar" basedir="bin">
    <manifest>
    <attribute name="Class-Path" value="lib/lib1.jar lib/lib2.jar lib/lib3.jar"/>
    <attribute name="Built-By" value="me"/>
    <attribute name="Main-Class" value="mypackage.Myclass"/>
    </manifest>
</jar>

This will add all the appropriate entries to the Manifest file. In order to be able to run the jar, you also need to create a lib folder and place all the dependency jars there:

myjar.jar
lib/lib1.jar
lib/lib2.jar
lib/lib3.jar
kgiannakakis
The attribute order seems to matter. The Class-Path needs to appear prior to the Main-Class.
dacracot
+1  A: 

Use eclipse plugin called "fatjar"

it's update-site

http://kurucz-grafika.de/fatjar

Just right-click on project and use fatjar option, next step allow you to choose which library will be included in *.jar

Maciej Kreft