Runtime JAR File in Java

The full form of JAR is Java Archive File. Java provides this feature to bundle multiple java program files as well as their corresponding class files into one single unit. Perhaps, it is the only file format that can store audio, video, text files and so forth in one place.

In this article, we will discuss how we can create and access a JAR file in Java.

Java JAR File

The need for data compression was felt to store and transmit large amounts of data without any hassle. For this purpose, a ZIP file was introduced that encouraged Java creators to develop JAR files. Their primary motive was to archive the Java applet and its components like classes, images and other resources so that it can be downloaded securely through the browser in only one HTTP request.

The two main benefits of JAR files are −

  • We can fetch multiple Java programs at once.

  • It also compresses the size of file which saves a lot of time required in downloading.

Since it is created using Java programming language, therefore it is also platform independent means a JAR file created on a device can be supported on other devices also.

Till this point, we have understood about Java JAR files. Now, let’s discuss how we can create and run it.

Creating JAR files through command line

Open your system’s command prompt and follow the below syntax to create a jar file −

Syntax of command

jar cvf nameOfjarfile nameOfinputFile

Here, ‘c’ signifies that we are creating a jar file. ‘v’ display information during creation of the jar file and ‘f’ tells us to store command output to the specified jar file.

‘nameOfinputFile’ denotes the files that are going to store in ‘nameOfjarFile’.


jar cvf Newfile.jar Stream.class 

The above command will create a jar file named ‘Newfile’ with .jar extension. The ‘’ and ‘Stream.class’ will get stored in ‘Newfile.jar’.

Running JAR files through command line

Step 1

Create a Java program in your IDE. Copy and paste the following code −

public class Var {
   public static void main(String []args) {
      int n1Data = 8;
      int n2Data = 15;
      int mult = n1Data * n2Data;
      System.out.println("Value of n1 and n2 multiplication is: " + mult);

In the above code, we have declared and initialized two variables and stored the result of multiplication in the ‘mult’.

Step 2

Compile it and generate its class file through the following command.


Step 3

Now create a manifest.txt file. We all know that the execution of a Java program starts from main() method and it is contained in a class. The manifest file contains the name of that class therefore, to run the jar file directly from command line we need a manifest file. It stores the class as a key value pair. Here, name of our class is ‘Var’ so we need to write ‘Main-Class: Var’ in ‘manifest.txt’ file.

Content of manifest.txt −

Main-Class: Var

Step 4

Type the following command −

jar cfm NewFile.jar manifest.txt Var.class

We can also create a jar file without manifest.txt but we need to specify the name of class in the command.

jar cfe File1.jar Var Var.class

Step 5

Now run the jar file by following command:

java -jar NewFile.jar


Value of n1 and n2 multiplication is: 120


Java Archive Files is actually a zip file that enhances the portability of Java files and also it makes storage efficient as it reduces the size of resources contained in it. In this article, we have explored Java JAR files including their creation and execution.

Updated on: 15-May-2023


Kickstart Your Career

Get certified by completing the course

Get Started