If you are Java programmer then you know what is the 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 produces 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.
Executable JAR files: JARs which can be executed as a standalone Java applications. The main
difference between normal and executable JAR file is that later contains a mainfest file, which specifies a main-class entry. When you run that JAR file, Java starts your application by reading that main-class entry, because you need the main method to execute Java programs.
difference between normal and executable JAR file is that later contains a mainfest file, which specifies a main-class entry. When you run that JAR file, Java starts your application by reading that main-class entry, because you need the 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.
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.
Select your project and click on the export option.
Step 2
Choose Java and select JAR file for exporting
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.
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.
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.
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.
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.
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.
No comments:
Post a Comment