Java : Generating Executable Jar File From Project Containing Third Party Library
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
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)
5. Expand file groups and check the filters. Click ok. This will create a deployment profile.
6. Once the deployment profile is created, right click on project, select Deploy -> DeploymentProfileName (Name of profile created in previous steps)
7. Click next, note down the 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
3. Select File Group and Click New
4. Give File Group Name and click ok
5. Open Contributors. Remove existing entries
6. Click add and browse for the jar file location on your file system
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.
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.