Quick Guide: How to Export an Eclipse Java Project as a Runnable Jar

Hey there, let's jump right into this, but before we start I must say, I've been using the eclipse IDE for several years now (it's actually eight years to be exact) and I must say, it has been quite a great journey so far with one of the powerful tools for developers.


Related: Tip No. 1 | Tip No. 2 | Tip No. 3 | Tip No. 4 | Tip No. 5 | Tip No. 6 | Tip No. 7 | Tip No. 8 | Tip No. 9 | Tip No. 10


What I want to talk to you about here is a feature that should be very useful to those of you who would like to export their Java SE projects into runnable jars with so little hassle!

During your development, you'll most probably create a Run Configurations. Those configurations are what you use to run your application from within Eclipse, they have all there is to need in terms of runtime parameters and dependency Jar references. (See image below to know how to access the Run Configuration).

Accessing the 'Run Configuration' menu
Accessing the 'Run Configuration' menu

What Eclipse offers on the table here is the ability to use this Run Configuration as the basis for the export process as opposed to exporting normal Jar files relieving you from having to manually creating the MANIFEST.MF file.

In order to do that, right click on your project and choose Export from the context menu. You will then be presented with a dialog hosting a list of various export features, choose 'Runnable Jar file' form the list under 'Java' and click 'Next' (See image below).

Exporting project to a 'Runnable JAR file'
Exporting project to a 'Runnable JAR file'

Now, you will see a dialog asking you to choose the Run Configuration to export, the project you chose to export from will be selected by default, but that does not mean you cannot export another one in the same workspace in case you chose the wrong project. That is all there is to it!

Configuring the 'Runnable JAR file'
Configuring the 'Runnable JAR file'

Before we finish this quick guide, it is important to highlight the different approaches provided for dealing with third-party libraries that might be associated with your project.

The first approach 'Extract required libraries into generated JAR' extracts the jar files of all of your dependencies and packages all of their class files and resources into your Jar file avoiding the need to add any special entries in the MANIFEST.MF file. While, this might be okay if the dependencies are your own, you have to be careful with third-party libraries because of licensing constraints.

The second approach 'Package required libraries into generated JAR' will place all dependencies into your Jar file and reference them int he MANIFEST.MF file.

Finally, the last approach will place all dependencies into a folder adjacent to your exported Jar file and have all libraries referenced in the MANIFEST.MF file. As you might have noticed the last two approaches are quite similar in concept, the location is only different.

Have fun!

Comments

Popular posts from this blog

Tech Tips Digest: 2