Java and Spring Level 1 This level · 76 of 105
Easy java spring

Difference between a fat JAR and a normal JAR?

The 30 second answer

A normal application JAR usually contains my classes and resources, so I still need to provide its dependencies at runtime. A Spring Boot executable JAR packages those dependencies as nested JARs alongside the application and includes a launcher. The manifest points to JarLauncher, which creates a class loader that can read those nested dependencies and then invokes my application's main method. That makes the application runnable as one artifact without assembling an external classpath.

Could you say this in an interview?

1. Layout.

app.jar
 ├─ META-INF/MANIFEST.MF
 │    Main-Class: org.springframework.boot.loader.launch.JarLauncher
 │    Start-Class: com.shop.App
 ├─ org/springframework/boot/loader/...   (loader classes)
 └─ BOOT-INF/
     ├─ classes/          (your code and application.yml)
     ├─ lib/              (dependency jars, not unpacked)
     ├─ classpath.idx     (lib order)
     └─ layers.idx        (for Docker layers)

2. Why a special loader is needed. The standard JDK class loader cannot load classes from a JAR inside a JAR. Shading tools solve this by unpacking all dependencies into one JAR, which can break files with the same name (for example META-INF/services). Spring Boot keeps dependency JARs intact and adds its own loader instead.

3. How it starts. The launcher decides what goes on the classpath:

protected boolean isLibraryFileOrClassesDirectory(Archive.Entry entry) {
    String name = entry.name();
    if (entry.isDirectory()) {
        return name.equals("BOOT-INF/classes/");
    }
    return name.startsWith("BOOT-INF/lib/");
}

Launcher.java L171-L177

and finds your main class in the manifest:

String mainClass = (manifest != null) ? manifest.getMainAttributes().getValue(START_CLASS_ATTRIBUTE) : null;
if (mainClass == null) {
    throw new IllegalStateException("No 'Start-Class' manifest entry specified in " + this);
}

ExecutableArchiveLauncher.java L53-L60

Then it creates a class loader over those nested entries and calls your main method.

4. How it is built. The Spring Boot Maven plugin (repackage goal) or Gradle plugin (bootJar task) builds the normal JAR and repackages it. Maven keeps the original as app.jar.original.

Trade-offs:

  • Pros: one file to deploy, no classpath setup, easy containers.
  • Cons: bigger file; and in Docker, one change rebuilds a large layer. Spring Boot's layered JARs (layers.idx, java -Djarmode=tools -jar app.jar extract --layers) split dependencies and app code into separate image layers.
  • A fat JAR cannot be used as a normal library dependency, because the classes are under BOOT-INF/classes.

Interview distinction: WAR packaging is also supported for external servlet containers, but the fat JAR with an embedded server is the default.

Sources (3)
  1. Spring Boot reference: The executable jar format, nested jarsdocs.spring.io
  2. Spring Boot reference: Launching executable jarsdocs.spring.io
  3. Spring Boot reference: Efficient deployments (extracting the jar)docs.spring.io
Previous
Next