What is a JAR file?
A file with .jar extension is a Java Archive file that contains many different application related files as a single file. This file format was created to reduce the speed of loading a downloaded Java Applet in browser via HTTP transaction, by avoiding to create multiple HTTP connections. A single JAR file can contain Java class files (.class), images, and sounds. Individual items inside a JAR file can be digitally signed by the application developer to authenticate their origin. JAR files are regularly used to create functional APIs that contain specific functionality related to that API.
JAR File Format
JAR files are based on the popular ZIP file format that archives its individual content files in a single file. JAR file format supports compressions, resulting in a smaller file size for downloading and hence further improves the download time. The JAR file specifications artilce by Oracle gives complete details about the internal specifications of JAR files.
The Manifest File
When a JAR file is created, a manifest file is automatically created inside it that contains the metadata information about the contents of the JAR file. This file shows the contents that are packaged inside the JAR file. A typical Manifest file looks as follow which shows the folders and classes included in the JAR file.
META-INF/
META-INF/MANIFEST.MF
pack/
pack/class1.class
pack/class2.class
..
..
Manifest Specifications
Manifest specifications are defined by Oracle as follow.
manifest-file
: main-section newline *individual-section
main-section
: version-info newline *main-attribute
version-info
: Manifest-Version : version-number
version-number
: digit+{.digit+}*
main-attribute
: (any legitimate main attribute) newline
individual-section
: Name : value newline *perentry-attribute
perentry-attribute
: (any legitimate perentry attribute) newline
newline
: CR LF | LF | CR (not followed by LF)
digit
: {0-9}
Executable JAR
JAR files can also comprise of an application that can be executed by Java Virtual Machine (JVM) similar to any other application on Microsoft Windows Operating System. In this case, the JVM needs to know about the application’s entry point that is any class with a public static void main method. The manifest file identifies such a class with Main-Class
header in the following format:
Main-Class: com.example.MyClassName