Generating an Executable JAR File From Project Containing Third Party Library
Join the DZone community and get the full member experience.
Join For Freea 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
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
5. expand file groups and check the filters. click ok. this will create a 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)
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.
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
java : generating executable jar file from project containing third party library
3. select file group and click new
select file group
4. give file group name and click ok
file group name
5. open contributors. remove existing entries
open contributors.
6. click add and browse for the jar file location on your file system
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
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
Published at DZone with permission of Yashwant Golecha, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments