Over a million developers have joined DZone.

Generating an Executable JAR File From Project Containing Third Party Library

· DevOps Zone

The DevOps Zone is brought to you in partnership with Sonatype Nexus. The Nexus Suite helps scale your DevOps delivery with continuous component intelligence integrated into development tools, including Eclipse, IntelliJ, Jenkins, Bamboo, SonarQube and more. Schedule a demo today

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

The DevOps Zone is brought to you in partnership with Sonatype Nexus. Use the Nexus Suite to automate your software supply chain and ensure you're using the highest quality open source components at every step of the development lifecycle. Get Nexus today

Topics:

Published at DZone with permission of Yashwant Golecha, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}