How to Create Jar File


Introduction

Jar stands for Java Archive which is a file format used to aggregate java classes and other resources such as images or text into one package.

We can create a jar file in several ways. For the purpose of this tutorial, we will be creating a jar file with the help of the command prompt.

Step 1 – Open the command prompt

First of all, open the run command by pressing the “windows + r” key. Over there, type in cmd and hit the enter key in order to open the command prompt.

Go to the Command prompt

Step 2 – Change the Directory

In the command prompt, go to the directory where the java classes are kept. In order to do so, type the “cd” command followed by the desired directory name.

For the purpose of this tutorial, we will go to the directory named as “java classes”.

In order to check the files in this folder, enter the “dir” command. You will notice some java files present with “. java” extension in this directory. These are the files of which we are going to make a jar file.

Use the Dir Command

Step 3 – Enter the jar –cf command

Simply type the “jar – cf myfile .jar *. java” command and then hit the enter key.

In this command, jar is telling the java compiler to create jar file, cf is an attribute of create file, myfile.jar is the name of our output file and the *.java is the extension of the files which will be packed in the archive.

After done with that, simply open the directory listing again by dir command to see whether the file has been created or not. And you will notice that a file with .jar extension will appear with the same name we used in the command.

Create a jar file

Step 4 – View the jar file

Let us open up the directory through windows explorer, right click on the jar file and open its properties.
It can be seen that this type of the file is an Executable Jar file. And this was all about creating a jar file.

Open the properties of the jar file