What is a JAR file?

Java 8Object Oriented ProgrammingProgramming

A java archive file is a file format/ archiving tool which contains all the components of an executable Java application. All the predefined libraries are available in this format.

To include any of these (other than rt.jar) in to your project you need to set the class path for this particular JAR file. You can create a JAR file using the command line options or using any IDE’s.

Creating a Jar file

You can create a Jar file using the jar command as shown below.

jar cf jar-file input-file(s)

Let us consider an example, create a Sample Java program with name Sample.java

Sample.java

public class Sample {
   public static void main(String args[]){
      System.out.println("Hi welcome to Tutorialspoint");
   }
}

If you compile this program using Javac command as shown below −

C:\Examples >javac Sample.java

This command compiles the given java file and generates a .class file (byte code)

Byte Code

Now, create a jar file for the generated class as −

C:\Sample>jar cvf sample.jar *.class
added manifest
adding: Sample.class(in = 434) (out= 302)(deflated 30%)

This will generate a jar file for all the classes in the current directory (since we used * instead of name) with specified name.

Jar File
raja
Published on 29-Dec-2017 10:40:09
Advertisements