Java : Generating Executable Jar File From Project Containing Third Party Library

By Yashwant
Mar 23rd, 2014
4 Comments
8647 Views

A lot of times we write java utility programs which can be used as stand alone applications. We package them as executable jar file  which could be shared with end users. If our code does not use any third party library, this process is kind of straight forward but when we have a third party library dependency, it gets a bit tricky. So in this post I will explain creation of jar file with inclusion of third party jar dependency. I am using JDeveloper IDE for reference here but process would be more or less same on other IDEs like Eclipse.

Here are the steps to create an executable jar file

1. Let’s assume you have written and tested your code. Now right click on your project in JDeveloper and select Deploy -> New Deployment Profile.

2. Selct profile type as JAR File. Give a name to your deployment profile and click ok

Create Deployment Profile

Java : Generating Executable Jar File From Project Containing Third Party Library 

3. On next screen, select Compress Archive and keep compression level as Default.

4. Check Include Manifest File (META-INF/MANIFEST.MF) This is the most important step as it will generate the Manifest file which will make the jar file executable. Select the Main class whose main method should be called when jar is executed. (You can think this as starting point of your application)

Include Manifest File

Include Manifest File

5. Expand file groups and check the filters. Click ok. This will create a deployment profile.

deployment profile

deployment profile

6. Once the deployment profile is created, right click on project, select Deploy -> DeploymentProfileName (Name of profile created in previous steps)

DeploymentProfileName

Java : Generating Executable Jar File From Project Containing Third Party Library

7. Click next, note down the path of deployed jar file.

 path of deployed jar file.

path of deployed jar file.

8. Open the path mentioned in above steps and open that location. You will find your jar file which could be shared with other users.

So above steps are good enough when you don’t have a third party library dependency. Now we will include third party library jar as part of our final jar only. You can either create a new deployment profile or edit the existing one.

1. Right click project and select Project Properties -> Deployment Profiles -> Profile to be edited

2. Click edit

 Click edit

Java : Generating Executable Jar File From Project Containing Third Party Library

3. Select File Group and Click New

Select File Group

Select File Group

4. Give File Group Name and click ok

File Group Name

File Group Name

5. Open Contributors. Remove existing entries

Open Contributors.

Open Contributors.

6. Click add and browse for the jar file location on your file system

browse for the jar file location

browse for the jar file location

7. Open Filters tab

8. This is the most important step in case of third party libraries. Uncheck MANIFEST.MF When final jar is created if there are two manifest files, it would be a contradiction, so we need to remove the manifest of third party library in our jar.

Uncheck MANIFEST.MF

Uncheck MANIFEST.MF

9. Click ok

10. Redeploy your project. This time you will notice that resultant jar file is a bit larger in size than previous one as it will contain classes from third party library jar also.

That’s all you need to create an executable jar. Go ahead and generate some utility jars which can be useful for everyone.

P.S. – Command to run executable jar file  java -jar FileName.jar

Do visit other Java Tutorials.

About the Author

- An avid reader and a big time comics fan. A software engineer by profession who loves writing code and make things work.

4 Responses to “Java : Generating Executable Jar File From Project Containing Third Party Library”

  1. AllHailAndrejusOurLordAndSavior says:

    When I have done this in jdev, I used the uberjar and manifest maven plugins in jdev, but if you rely on jws/jpr deployment plans (and we do), this is awesome. No more eclipse jar exports and no more jarjar. Good!

  2. Andrew says:

    Thank you so much!! this really helps!

    I had a question about this though. When I build a 5 MB JAR file it takes almost 2 hours to complete. Is there anyway to make it go faster?

  3. Hey there, You’ve done an incredible job. I will definitely digg
    it and personally recommend to my friends. I’m sure they’ll be
    benefited from this site.

  4. jose says:

    Hello, I have always developed my java NetBeans projects. When I finish my project, and automatically build “dist” folder where the main jar and a “lib” folder containing all the external jar was created. This is done automatically with the NetBeans IDE

    Now I’m trying to do a project with jdeveloper 12c Java Desktop Application and try to deploy my project but I have problems with java.lang.NoClassDefFoundError. I meet several questions:

    Can you add the same structure as I said before? That is, a file jar and inside a folder “lib” place the jars using How to do it?

    I’ve seen examples that by New File Group adds refencias, but it really adds to the main jar file (as I see in the size of the file) but not saved in a separate folder.

    I do not know if you can help me do this in jdeveloper 12c: Generate a jar file and folder lib with references.

    Thank you for your help.

Leave a Reply to AllHailAndrejusOurLordAndSavior Cancel reply

Your email address will not be published. Required fields are marked *

facebook comments: