Saturday, April 5, 2014

How to Make Executable JAR file in Eclipse IDE - Java

If you are Java programmer then you know what is purpose of JAR file, but for those who are unaware, JAR file is deliverables of Java application. Just like C and C++  application produce EXE files, Java produce JAR files. In other words, A JAR (Java Archive) file is a ZIP format file that bundles Java classes into a single unit, it may contain all the resource needed by Java application as well. There are mainly two types of JAR file in Java:  Library JAR (normal JAR) files: JARs which are reusable libraries e.g. Apache commons JAR file, guava.jar itself or even JDBC drivers e.g. ojdbc6_g.jar. There is another type as well, Executable JAR files: JARs which can be executed as a standalone Java applications. Main difference between normal and executable JAR file is that later contains mainfest file, which specify a main-class entry. When you run that JAR file, Java start your application by reading that main-class entry, because you need main method to execute Java programs. I had earlier shared steps to create JAR file from command prompt, which if you have not read, go read it. You will learn about basics of JAR command, which comes with JDK, mainfest file and different attributes of manifest file. In this tutorial we will learn how to make/create/export both library and executable JAR file in Eclipse IDE. Why it's important to know creating executable JAR in Eclipse, because its one of the most used tool by Java programmer. Once you know the steps, you export your Java program as JAR file in a second or two. After creating executable JAR file, you can follow these steps to run Java program from JAR file in command line.



Steps to Create Executable JAR file in Eclipse

A picture is worth more than a thousand words, that's why I have provided all steps by taking screenshots. You can quickly learn how to create executable JAR for your Java application in Eclipse, by just looking at these screenshot. They are arranged from start to end. Which means first image is for first step and last image is for last step. By the way, if your application is dependent upon some other JAR files e.g. third-party library, then don't need to include them inside JAR. All you need to do is include them inside your Java Classpath. Java is smart enough to pick classes from those JAR. Also remember to include main method or entry point of your Java program, while creating executable JAR, as you can not run a Java program without main method, as discussed here.


Step 1
Select your project and click on the export option.




Step 2
Choose Java and select JAR file for exporting


Step 3
Now select the source and resources you want to export into JAR file. After selecting src folder and any resource, select the destination folder, where you want your JAR file to be created. Also don't forget to check option "compress the content of JAR file". This will reduce size of executable JAR file. If you want you can finish the process in this step, but you can also go one step further to save these instruction of executable JAR file creation for future use.


Step 4
In this step you can save all instruction into a "JAR description" file for future use. This will create a description file e.g. Test.jardesc in the chosen folder.


Step 5
This is an important step, because you are going to generate the manifest file, don't forget to choose main class. This is important to make your JAR an executable JAR file. Remember main method is entry point of any Java application.


Step 6
You are done with creating executable JAR file. Now you can go to the target folder, which you have chose in previous steps to see both JAR file as well .jardesc file to recreate JAR file again and again. This time you don't need to include main class as well, as those are already included.


Step 7
After making code changes, if you want to create a new JAR file, you don't need to go through all previous steps. This time, just select your JAR description file and say "create JAR", a right click menu option in Eclipse. This will create another JAR file in same folder. You can double check timestamp of JAR file.



That's all about How to create or make Executable JAR file in Eclipse IDE. By following these steps you can export your Java program as executable JAR, which allow you to share your program with your user, client and any one who wants to take a look. I strongly suggest to save instruction to export JAR file, so that you don't need to run through all these steps again and again. Next time, when you update your code, just click on your jardesc file and your JAR file will be ready in a blink.

5 comments:

  1. love this blog so much.. thank you javin paul for beng a blessing to me.. I would love to see more on data structures and algorithms in java.. thanks alot

    ReplyDelete
    Replies
    1. Thank You Anonymous. I will surely write some more tutorials and interview question experience on data structures and algorithms in Java. Stay tuned.

      Delete
  2. hey admin hi.., m just a beginner in Java, i just want to code a java transliteration programme (from English to Hindi).This is my project task and i really don't know where to start from.Please help me.

    ReplyDelete
  3. Thanks, it worked.

    ReplyDelete
  4. Finally I found, very very thanks

    ReplyDelete

Java67 Headline Animator