Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create an executable jar with dependency jars

Tags:

java

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.

like image 535
Sunil Kumar Sahoo Avatar asked Sep 29 '09 11:09

Sunil Kumar Sahoo


People also ask

How can I create an executable jar with dependencies?

Use the maven-shade-plugin to package all dependencies into one über-JAR file. It can also be used to build an executable JAR file by specifying the main class.

What is the difference between jar and executable jar?

FYI: In simple terms, the difference between a JAR file and a Runnable JAR is that while a JAR file is a Java application which requires a command line to run, a runnable JAR file can be directly executed by double clicking it.


2 Answers

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
like image 121
kgiannakakis Avatar answered Oct 16 '22 16:10

kgiannakakis


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

like image 21
Maciej Kreft Avatar answered Oct 16 '22 17:10

Maciej Kreft