Skip to content
Related Articles

Related Articles

Improve Article

Runtime JAR File in Java

  • Last Updated : 26 May, 2021

JAR stands for Java Archive file. It is a platformindependent file format that allows bundling and packaging all files associated with java application, class files, audio, and image files as well. These files are needed when we run an applet program. It bundles JAR files use a Data compression algorithm. These jar files can be manipulated using zip programs like WINZIP or WINRAR. 

It enables ease of distribution as all class files are packaged/bundled together and distributed to client applications. as one single jar file. These jar files can be signed by an author using digital certificate. These jar files can be authenticated using digital certificates and checking the author’s signature. The JRE (Java Runtime Environment) loads the classes from the JAR file without un-jarring it.

Methods: There are 2 ways of creating a JAR file.

  1. Using IDE
  2. Using the command line

Let us do discuss both of them in depth.

Method 1 – Using IDE

1.  The creation of a JAR file through an IDE like Netbeans or Eclipse is pretty straightforward. In File, we have an export option that helps us to export the java application as a JAR file. After this go to



File -> Export->Java-> JAR file

2. Now in a JAR file specification dialog, specify the resources/files to be included in the JAR file. The export destination is the location where the jar file is to be created.

3. Click on the Finish button

4. We then provide the location where we wish to have the jar file created.

5. It is pictorially depicted below in two snapshots which are as follows:

Method 2: Using the command line

Using the jar tool, we can create a jar file as below

cmd>> jar cvf jarfile inputfileDir1 inputfileDir2

Here,



  • c – create a new jar file
  • v – verbose mode which displays the messages while the jar file is created.
  • f –  bundles into a jar file specified by the parameter jarfile instead of standard output.
  • inputfileDir1, inputfileDir2 – indicates the input files which are to be bundled together in the jar file.

Now we will be manifesting the file as this is a special file that is bundled in your JAR file. It has special metadata like main class name, version control, the digital signature of the author, the java version used for bundling the jar file. The file name is “MANIFEST.MF” and it is a part of the META-INF subdirectory. If this file is not provided during bundling of the JAR file, it gets created automatically. When we extract and open the jar file, we can view this file. It has the following details

Manifest-Version: 1.0

The jar file can be run by the java application directly if has a manifest file with the header as Main-class. The Main-class header has the fully qualified name of the class which has the main(). This specifies the entry point of the application.

Illustration: Consider creating a JAR file with a manifest File called helloworld.MF

Manifest-Version : 1.0
Main-class : com.sample.test.HelloWorld

Example:

Java




// Importing the package 
package com.sample.test;
  
// Main class
public class HelloWorld {
  
    // Main driver method
    public static void main(String[] args)
    {
  
        // Print statements only
        System.out.println("Welcome to helloworld");
        System.out.println("Jar file to be created");
    }
}

Keep a note here m indicates the manifest file to be included while bundling into a jar file

Output:

Note: Here we compile the java file ‘HelloWorld.java’ using javac command. This command compiles the .java file and creates a class file HelloWorld.class. We then create the jar file helloworld.jar using jar command.

Lastly now while running the JAR file we use the below command to run the created jar file. When we run the jar file, ‘Helloworld‘ class which has main() gets loaded by the JVM, and the code gets executed.

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :