JAR (Java Archive)

A JAR (Java ARchive) is a ZIP file containing compiled .class files, resources and a manifest. JARs are the standard distribution format for Java libraries and applications. Executable JARs declare a Main-Class in the manifest and run with java -jar.

Creating a JAR

javac -d classes src/*.java
cd classes
jar --create --file=app.jar --main-class=com.example.App com/

Or with Maven / Gradle, the package / jar task does this for you.

Manifest

Every JAR has a META-INF/MANIFEST.MF. For executable JARs it includes:

Manifest-Version: 1.0
Main-Class: com.example.App
Class-Path: lib/postgresql.jar lib/jackson-core.jar

Running

java -jar app.jar
java -cp app.jar com.example.OtherMain   # run a different main class

Inspecting

jar tf app.jar           # list contents
jar xf app.jar           # extract all files
unzip -l app.jar         # JAR is a ZIP, any zip tool works

Fat JAR / uber JAR / shaded JAR

A fat JAR bundles your application and all its dependencies into one file, so java -jar works without setting classpath. Spring Boot produces fat JARs by default. Maven Shade Plugin and Gradle Shadow Plugin are common tools.

WAR and EAR

WAR (Web ARchive) is a JAR with a specific layout for web applications. EAR (Enterprise ARchive) bundles multiple WARs and JARs for Java EE application servers. Both are legacy — modern apps ship as fat JARs or container images.