How To Create JAR File In Java Using Eclipse

Hello coders, welcome to my new tutorial, How To Create JAR File In Java Using Eclipse. In this tutorial, you will learn to create normal jar files as well as executable jar files using eclipse IDE, and you will also learn the basics of jar files and why do we need to use jar files. So let’s get started

How To Create JAR File In Java Using Eclipse

Creating a jar file in Java using Eclipse IDE is very easy. It will take some simple steps to create a jar file. I will explain them in a detailed way so that you can understand it very well. But before going further to our main topic – How to create a Java JAR file? We have to know about jar files and why do we use them.

So let’s continue this tutorial !!!

What is jar file?

  • Jar stands for Java-archive, which is a compressed format of a compiled Java project.
  • It is a file format based on the popular ZIP file format and is used for aggregating many files into one.
  • In other words, a JAR file is a file that contains a compressed version of .class files, metadata and some other resources.
  • JAR files end with the .jar extension.

Types Of JAR Files

  • Library JAR (normal JAR) files : Normal JAR files are the collection of compiled Java classes.
  •  Executable JAR files : Executable JAR files are also the collection of compiled Java classes but with the main class. To create an executable jar file, we must have to specify the Main Class in the manifest file.

We will discuss all the steps of creating normal and executable jar file in further section. So keep reading till the end.

Why do We Need to Use Jar Files?

  • If we are working on multiple projects and there is one feature which is common or used in all projects, then we can use a jar file.
  • It is used to distribute Java codes/libraries for use by other projects.
  • We can also use the jar file to execute the Java program directly without any IDEs.
  • The JAR format also supports compression, which reduces the size of the file and improves download time further.
  • We can use jar files for tasks such as lossless data compression, archiving, decompression, and archive unpacking. 

Now let’s start our main topic, how to create jar file from Java project in eclipse.

How To Create Library JAR (normal JAR) File In Java Using Eclipse

So here are the steps which are followed to create jar file in eclipse. First of all, open your eclipse workspace and then follow the following steps.

Step 1. Select and right-click on your project name in the Project Explorer view and then select the Export option.

How To Create JAR File In Java Using Eclipse
How To Create JAR File In Java Using Eclipse – fig – 1

Step 2. Expand the Java folder, select the JAR file, and click on the Next button.

How To Create JAR File In Java Using Eclipse
How To Create JAR File In Java Using Eclipse – fig – 2

Step 3. Now you are in JAR File Specification window.

  • Here Under the section, Select resources to exportselect the source and resources you want to export into your JAR file.
How To Create JAR File In Java Using Eclipse
How To Create JAR File In Java Using Eclipse – fig – 3
  • Now under section Select the export destination, specify the location where you want to export your jar file.
  • And under section Options, check the check box of compress the contents of the jar file.
  • Then click on the next button, it will take you to the JAR Packaging Option window.
How To Create JAR File In Java Using Eclipse
How To Create JAR File In Java Using Eclipse – fig – 4

Step 4. Now you are in JAR Packaging Option window.

  • Here under section Select options for the JAR export, check both checkboxes. You can see it in below screenshot.
  • Now check the option Save the description of this JAR in the workspace and click Browse button to specify the location where you want to save the description of this JAR file so that you can use this file to create JAR file whenever you want to create a new JAR file.
  • If you make JAR description file then you will not have to follow all the above steps to create a new JAR file every time.
fig – 5
  • Now click on the Finish button, and our jar file has been created. Let’s see in the below image.
fig – 6
  • You can see our HelloWorld jar file has been created.

How To Create Executable JAR File In Java Using Eclipse

To create executable jar file, we have to follow the same steps as we followed to create a normal jar file except for one more step.

  • In JAR Packaging Option window, click next button, it will open JAR Manifest Specification window. This will generate the manifest file for jar file.
  • Here, under section Specify the manifest, check Generate the manifest file option. Now under section Seal contents, check the Seal the JAR option.
  • Now under section Select the class of the application entry point, you have to specify your main class.

Specifying main class is important to make your JAR an executable JAR file because as we all know the main method is the entry point of any Java application.

You can see them in below screenshot. Then click on the finish button, it will create executable jar file in the specified location.

fig – 7
fig – 8

So guys, I am wrapping up the How To Create JAR File In Java Using Eclipse tutorial here. I hope you have found this detailed guide with screenshots very helpful. But anyway, if you have confusion in any steps, feel free to ask your queries in the comment box. If you like, make sure to SHARE this tutorial with your Java fellows. THANKS 🙂

People are also Reading…..

Leave a Comment