8

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

Marwan Ansari
  • 123
  • 1
  • 10
Sunil Kumar Sahoo
  • 53,011
  • 55
  • 178
  • 243

6 Answers6

5

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
  • 103,016
  • 27
  • 158
  • 194
2

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

1

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
  • 2,601
  • 3
  • 22
  • 26
1

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

This page should give you a good starting point.

developmentalinsanity
  • 6,109
  • 2
  • 22
  • 18
0

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

Rich Seller
  • 83,208
  • 23
  • 172
  • 177
  • Just to save other time, IzPack is a deployment tool that installs the main jar and the other jars somewhere on a computer, while JSmooth takes all those jars and encapsulates them in with an EXE file so the application can run on Windows. – Lee Meador Jul 17 '18 at 15:29
0

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,481
  • 12
  • 29