JAR is an abbreviation for Java Archive. It is basically a file in archive format used to group and integrate Java classes, resources, metadata together which can then be easily distributed across Java platform.

However the underlying file format is ZIP with a .jar extension. Every JAR file contains a manifest file which is normally the first in the JAR header. The Java Development Kit (JDK) bundles an built-in tool to create and extract JAR files.  Like any archive format, JAR also helps compress data that’s packed into it.

The key advantage of wrapping everything into a single JAR file is that it is easy to distribute and can be downloaded via a single HTTP request over the Internet (specially Applets). It is also an open standard and fully extendable format.

The manifest file basically tells the Java Virtual Machine (JVM) what all things are packed into the archive and which file contains the starting class/method called main.

Executing a JAR File

A JAR file can also be directly executed, but the method may differ on different platforms. Any executable Java program, along with related libraries and resources, can be bundled into a JAR file. The differentiating factor for executable and non-executable JAR files is that executable JAR file has an extra field in the first file with a hexadecimal code – 0xCAFE. Moreover, the following must be specified in the manifest file for the main class.

Main-Class: myPrograms.MyClass

To run a JAR file, you can either run it like any other executable file or run this command: java -jar example.jar. A JAR can also be wrapped into an .EXE file for Windows users with tools like Launch4J, WinRun4J and JSmooth. Else, the Java Runtime Environment (JRE) takes care of executing JAR on Windows.

JAR File Specifications

The specifications mentioned for a JAR file involve the following:

  1. Manifest Specification
  2. Main Attributes
  3. Per-Entry Attributes
  4. Signature File
  5. Signature Validation
  6. Magic Attribute
  7. Digital Signature
  8. Index File Specifications
  9. Provider Configuration File
  10. Acknowledgements

JAR Hell

JAR Hell is a term that specifies all the ways a particular JAR may not work. It is very similar to the term DLL Hell. A JAR file can fail for below mentioned reasons:

  • Creating multiple versions of the same library.
  • Different libraries require different versions of some third-party library in the same execution module.
  • Unexpected behavior in class loading.