In a nutshell
A JAR (Java ARchive) is a package file formated used to combine many Java class files, metadata, and resources (e.g., images, property files, etc) into a single compress file that can be easily distributed. It’s essentially a ZIP file.
Key features:
- Java class files: multiple
.class
files (Java bytecode) produced after compiling.java
source files. - Manifest file: Usually includes a
META-INF/MANIFEST.MF
file that can define various attributes like the main class to be executed (for standalone apps) and other metadata. - Resources: JAR files can package resources such as images, text files, property files, and configuration files, which can be accessed by Java classes during runtime.
- . Compression: JAR files use the same compression algorithm as ZIP files, which helps in reducing the file size and makes it easier to distribute over networks.
Executable vs Library JARs
An executable JAR has a Main-Class
entry in its manifest, allowing it to be run directly from the terminal using a command like;
java -jar myjar.jar
A Library JAR contains Java libraries (classes and resources) that are intended to be included as dependencies in other Java applications, instead of being ran directly as with executable JARs.