Skip to content

JAR

This content is for Java. Switch to the latest version for up-to-date documentation.

JAR (Java Archive) is a package file format used to aggregate multiple Java class files, associated metadata, and resources (text, images, etc.) into one file for distribution.

A JAR file is essentially a ZIP file with a .jar extension that contains compiled Java classes and resources needed for a Java application or library.

The basic syntax for creating a JAR file using the command line:

Terminal window
jar cf jar-file input-files

Where:

  • c indicates you want to create a new archive
  • f specifies the archive filename
  • jar-file is the name you want to give your JAR file
  • input-files are the files you want to include

Example:

Terminal window
jar cf myProgram.jar *.class

JAR files can include a special META-INF/MANIFEST.MF file that contains metadata about the JAR:

Manifest-Version: 1.0
Main-Class: com.example.MainClass

To create a JAR with a manifest:

Terminal window
jar cfm myProgram.jar manifest.txt *.class

To create an executable JAR:

  1. Create a manifest file with Main-Class specified
  2. Include the manifest when creating the JAR

Example manifest (manifest.txt):

Main-Class: com.example.MainClass

Creating the executable JAR:

Terminal window
jar cfm myProgram.jar manifest.txt *.class

Running the JAR:

Terminal window
java -jar myProgram.jar

To list the contents of a JAR file:

Terminal window
jar tf myProgram.jar

To extract the contents of a JAR file:

Terminal window
jar xf myProgram.jar
Built with passion by Ngineer Lab