Welcome to a new post in Java swing tutorial series. This post will gonna teach you how to create a jar file in java. A jar file is a java executable file which is already compiled. That means you no need to compile your java program again and again once jar file is created. Simply double-click on the created jar file (.exe), it will open your program. So in this tutorial will know what it is and how to create it? So let’s start. You can go through my previous tutorial in which I’ve created a calculator. The description is given below.
Now, first of all, we should know about the jar file so let’s know.
What is a Jar File
- The JAR stands for Java Archive. It is a file format that is based on the popular ZIP file format or compressed file.
- As we know in a zip file we aggregate several files into a single file or folder.
- We create a jar of the java application files which is called .exe file.
- The main advantage of creating a jar of java file is that you no need to compile and run your program again and again. Instead of it if you have created a jar executable file then simply run it.
- If you want to give any demo of your java program to someone then simply create its jar executable so that you no need to show your actual program code to everyone.
- A jar file contains several .class files, images, and sounds into a single file which are used in our Java project.
- It reduces the actual file size because it supports file compression so it is easy to portable.
Creating Jar File
- To create a jar file we need a java program (project) which contains the .class file.
- We can bind more than one class file in a single executable file.
- Here we will create a jar file of my previous tutorial in which I had created a Calculator.
1. Creating Jar File through NetBeans IDE
- First of all, open the NetBeans IDE and open the created project or if you want then you can create any new project as per your requirement.
- In this case, I’m going to use the existing project that I have created in the last post.
- Under the project tab, you will see your project. In my case, the myCalculator project is showing there.
- Right-click on the selected project and go to the properties.
- Under the Categories, option go to Build -> Packaging
- Now select all the options in the given list. The lists are as follows –
- Compress JAR File
- Build JAR after compiling
- Copy Dependent Libraries
- After selecting the above options click OK and finish it.
- Now again right-click on the project and click on the Clean and Build.
- It will take few seconds to build the executable file. You can see below in the output window the creation process of the jar file.
- Once it is finished just go the project directory where your project is saved.
- Open it and navigate to dist (distribution) folder.
- Double click on that and you will see the Executable Jar File of your project is created.
- Double click on that and run. Here the executable file is running and you no need to compile and run from the command prompt.
2. Creating Jar File through Command Prompt
- In another way, we can create an executable jar file through the command prompt. If you haven’t installed NetBeans IDE on your system then don’t worry you can create java executable file through the command too.
- First of all, open the notepad and create a text file in the same directory where your project is located.
Main-Class : ClassName
- Write the above code. (Just change the ClassName as your). In my case it is myCheckBox
- Now save it in the same directory of the project with the name manifest.txt
- Now open the command prompt and change the directory to the project directory where it is saved.
- Then compile the program with javac fileName.java
- After successful compilation of the program, the .class file will be generated as shown in the figure.
- The project folder should mainly consist the three files such as manifest, .class file and .java file.
- Now move again to the command prompt and type the following command.
jar cfn myCheckBox.jar manifest.txt myCheckBox.class
- Change the class name as your and hit enter.
- Now see in your project folder the java executable file is created. You can run it by double-click.
That’s all about the Java executable jar file. This will save your code and time by compiling and running the java program again and again. The above steps are easy to use and create the jar file in java. I hope this tutorial will help you much as you required. Thank you 🙂