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
Java Introduction
What is Java? JDK, JRE and JVM
Java Install and Setup
Your First Java Program: Hello World
Java Syntax
Java Comments
Java Cheat Sheet
Exercises & Assignments
No Content Found.
Interview Questions & Answers
What is the difference between Java and JavaScript?
What is Java?
What are the main features of Java?
What is the difference between JDK, JRE and JVM?
Why is Java platform independent?
What is bytecode in Java?
Why is the main method public static void in Java?