What is a JAR file in Java and how to create one?

A JAR (Java ARchive) file is a ZIP file which holds compiled .class files, resources and a manifest. It is the normal way to share a Java library or an application.

public class Hello {
    public static void main(String[] args) {
        System.out.println("Running from a JAR");
    }
}
Terminal

$ javac Hello.java
$ jar cfe app.jar Hello Hello.class
$ java -jar app.jar
Running from a JAR
$ jar tf app.jar
META-INF/
META-INF/MANIFEST.MF
Hello.class

  • c create, f file name, e entry point (the class with main).
  • jar tf lists the content of a JAR.

Real projects use Maven or Gradle to build the JAR.


Learn more about the similar topics:
Tutorials
No Content Found.
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.