Backend Development 7 min read

Why Can Spring Boot Fat JARs Run Directly? Uncover the Secrets

This article explains how Spring Boot packages applications as executable Fat JARs, embeds a web server, uses a special launcher, and defines a main class so the JAR can be run with a simple java -jar command, eliminating the need for separate deployment servers.

macrozheng
macrozheng
macrozheng
Why Can Spring Boot Fat JARs Run Directly? Uncover the Secrets

1. Fat jar

Unlike a regular JAR, a Spring Boot JAR is a Fat JAR that contains all project dependencies under

BOOT-INF/lib

and the compiled classes under

BOOT-INF/classes

. This means the JAR includes both the application code and every required library, so no external installation of dependencies is needed.

Typical Maven configuration for creating a Fat JAR uses the Spring Boot Maven plugin:

<code>&lt;project&gt;
    &lt;!-- ... other configurations ... --&gt;
    &lt;build&gt;
        &lt;plugins&gt;
            &lt;plugin&gt;
                &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
                &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
            &lt;/plugin&gt;
        &lt;/plugins&gt;
    &lt;/build&gt;
&lt;/project&gt;
</code>

Running

mvn package

creates a JAR that bundles all dependencies.

2. Embedded server

Spring Boot applications embed a web server such as Tomcat. The required server dependency is added in

pom.xml

or

build.gradle

:

<code>&lt;dependencies&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
        &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
    &lt;/dependency&gt;
    &lt;!-- ... other dependencies ... --&gt;
&lt;/dependencies&gt;
</code>

When the application starts, Spring Boot automatically configures and launches this embedded Tomcat.

3. Spring Boot starter

Every Spring Boot app has a main class with a

main

method annotated with

@SpringBootApplication

, which combines

@Configuration

,

@EnableAutoConfiguration

, and

@ComponentScan

:

<code>import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MyApplication {
    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }
}
</code>

Calling

SpringApplication.run()

starts the Spring context and the embedded server.

4. Executable JAR

The JAR’s

META-INF/MANIFEST.MF

specifies the main class (

Main-Class

). When you execute

java -jar your-app.jar

, the JVM reads this entry and invokes the

main

method.

In Spring Boot, the main class is

org.springframework.boot.loader.JarLauncher

, which loads the Fat JAR, creates a custom class loader, and launches the application:

<code>protected void launch(String[] args) throws Exception {
    JarFile.registerUrlProtocolHandler();
    // custom class loader loads JAR files
    ClassLoader classLoader = createClassLoader(getClassPathArchives());
    launch(args, getMainClass(), classLoader);
}

protected String getMainClass() throws Exception {
    Manifest manifest = this.archive.getManifest();
    String mainClass = null;
    if (manifest != null) {
        mainClass = manifest.getMainAttributes().getValue("Start-Class");
    }
    if (mainClass == null) {
        throw new IllegalStateException("No 'Start-Class' manifest entry specified in " + this);
    }
    return mainClass;
}
</code>

Thus, a Spring Boot Fat JAR is self‑contained, embeds its server, and defines a launcher, allowing it to be run directly without external containers.

JavamavenSpring BootExecutable JARFat JAREmbedded Server
macrozheng
Written by

macrozheng

Dedicated to Java tech sharing and dissecting top open-source projects. Topics include Spring Boot, Spring Cloud, Docker, Kubernetes and more. Author’s GitHub project “mall” has 50K+ stars.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.