Sub topic 13
Packaging an executable JAR
This chapter shows how to turn your Spring Boot project into a single file that runs with java -jar. It assumes you have a project from Spring Initializr, a JDK (17 or later for Spring Boot 3), and Maven. It also assumes you know what a JAR is and have run ./mvnw spring-boot:run before.
Why package the application
spring-boot:run is for development. It uses your build tool, your source tree, and the Maven repository on your machine. A server or container has none of those. It needs one file that contains your code and every library, and a JDK or JRE to run it.
Spring Boot calls this file an executable JAR, often called a fat JAR or uber JAR. It holds your compiled classes, all dependency JARs (including the embedded server), and a small loader. You do not install Tomcat or a framework on the target machine. You copy one file and start it.
Building the JAR
Run the build from the project root:
./mvnw clean packageclean deletes the target/ directory so old output cannot leak into the new build. package runs the Maven lifecycle through compile, test, and jar, and then Spring Boot's plugin rewrites the result. Tests run as part of package. If a test fails, no JAR is produced.
When the build finishes, look in target/:
ls target/*.jar*You should see two files, assuming the default artifact name from the Initializr example:
demo-0.0.1-SNAPSHOT.jaris the executable JAR.demo-0.0.1-SNAPSHOT.jar.originalis the plain JAR Maven built first, containing only your classes.
The name comes from the artifactId and version in pom.xml. If you change either, the file name changes.
Running the JAR
java -jar target/demo-0.0.1-SNAPSHOT.jarThe embedded server starts on port 8080 unless application.properties says otherwise. If you kept server.port=8081 from the configuration chapter, use that port in the call:
curl http://localhost:8081/helloStop the application with Ctrl+C. Spring Boot shuts the context down and closes the server cleanly.
How the build produces the fat JAR
The spring-boot-maven-plugin does the work. Initializr adds it to your pom.xml:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>The plugin has no version and no <executions> block. The Spring Boot parent POM manages the version and binds the plugin's repackage goal to the package phase. This is why a plain mvn package yields an executable JAR. If your project does not use the Spring Boot parent (for example, it imports the Boot BOM instead), you must set the version and add the repackage execution yourself.
What is inside the JAR
A JAR is a ZIP file, so you can list its contents:
jar tf target/demo-0.0.1-SNAPSHOT.jar | head -n 20The layout looks like this:
| Path | Contents |
|---|---|
| `META-INF/MANIFEST.MF` | Metadata, including `Main-Class` and `Start-Class` |
| `org/springframework/boot/loader/...` | The Spring Boot loader classes |
| `BOOT-INF/classes/` | Your compiled classes and resources such as `application.properties` |
| `BOOT-INF/lib/` | Every dependency as a nested JAR |
| `BOOT-INF/classpath.idx` | The classpath order the loader uses |
| `BOOT-INF/layers.idx` | Layer definitions for container images |
Read the manifest to see the entry points:
unzip -p target/demo-0.0.1-SNAPSHOT.jar META-INF/MANIFEST.MFTwo entries matter:
Main-Classnames the Spring Boot launcher,org.springframework.boot.loader.launch.JarLauncherin Boot 3.2 and later. Older versions useorg.springframework.boot.loader.JarLauncher.Start-Classnames your own class withmain, such ascom.example.demo.DemoApplication.
When you run java -jar, the JVM starts the launcher, not your class. The launcher builds a class loader that can read the nested JARs in BOOT-INF/lib, then calls your main method. A standard JVM cannot load JARs from inside another JAR, which is why this loader exists.
Changing the packaged result
Skipping tests
For a quick local build, skip tests:
./mvnw clean package -DskipTestsThis compiles the test classes but does not run them. Use -Dmaven.test.skip=true to skip compiling them too. Do not skip tests in your CI pipeline.
Setting the file name
Add finalName to the build section if you want a fixed name without the version:
<build>
<finalName>demo</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>The output becomes target/demo.jar. A fixed name makes deployment scripts simpler, but you lose the version in the file name.
Overriding configuration at launch
Properties from application.properties are packaged inside the JAR. You can still override them at start time without rebuilding:
java -jar target/demo-0.0.1-SNAPSHOT.jar --server.port=9090You can also use a JVM system property or an environment variable:
java -Dserver.port=9090 -jar target/demo-0.0.1-SNAPSHOT.jar
SERVER_PORT=9090 java -jar target/demo-0.0.1-SNAPSHOT.jarCommand-line arguments take precedence over system properties, which take precedence over environment variables, and all of them override the file inside the JAR. Note that JVM options such as -Dserver.port or -Xmx512m must come before -jar. Anything after the JAR name goes to your application as arguments.
A file named application.properties placed next to the JAR (in the working directory) or in a config/ subdirectory also overrides the packaged one. This lets operations teams change settings per environment while the JAR stays identical.
Gradle equivalent
If your project uses Gradle, the Spring Boot Gradle plugin adds a bootJar task. The build produces the executable JAR in build/libs/:
./gradlew bootJar
java -jar build/libs/demo-0.0.1-SNAPSHOT.jarThe plain JAR from the jar task gets a -plain suffix, so both files can coexist.
Layered JARs and containers
A fat JAR changes every time your code changes, even though the 40 MB of libraries inside it stay the same. In a container image, that wastes space and upload time, because the whole file is one layer.
Spring Boot JARs include layers.idx, which groups content into layers: dependencies, Spring Boot loader, snapshot dependencies, and application. In Boot 3.3 and later you can extract them:
java -Djarmode=tools -jar target/demo-0.0.1-SNAPSHOT.jar extract --layers --destination extractedEach layer becomes a directory. A Dockerfile can copy them one by one, so the rarely changing dependency layer is cached and only the small application layer is rebuilt. Boot 2.3 to 3.2 uses -Djarmode=layertools and the extract command without --layers. Check the documentation for your version.
Common errors
The following table lists the problems you are most likely to meet.
| Symptom | Likely cause | Fix |
|---|---|---|
| `no main manifest attribute, in demo.jar` | You ran the `.jar.original` file or a JAR built without the plugin's `repackage` goal | Run the file without `.original`. Check that the plugin is in `pom.xml` |
| `UnsupportedClassVersionError` | The JAR was compiled for a newer Java than the one running it | Run with the same or a newer JDK. Check `java -version` |
| `Port 8080 was already in use` | Another process, or an earlier run of your app, holds the port | Stop that process or start with `--server.port=` |
| Build fails during `package` with a test failure | A test failed, so no JAR is built | Fix the test. Use `-DskipTests` only for local checks |
| Old behavior after a code change | A stale file in `target/` or you ran an older JAR | Run `clean package` and check the file timestamp |
| `ClassNotFoundException` for a library you added | The dependency has `provided` or `test` scope, so it is not in `BOOT-INF/lib` | Use the default `compile` scope for runtime libraries |
Checking your work
After each build, a short routine catches most mistakes:
- Confirm the executable JAR exists in
target/and has a recent timestamp. - List its contents and check that
BOOT-INF/classescontains your classes. - Start it with
java -jarfrom a different directory, to prove it does not depend on your project folder. - Call
curl http://localhost:8080/hello(or your configured port) and check the responseHello, Spring Boot!.
Exercise
You run ./mvnw clean package and find both demo-0.0.1-SNAPSHOT.jar and demo-0.0.1-SNAPSHOT.jar.original in target/. Which one do you deploy, and what does java -jar do when it starts that file?
Show answer
Deploy demo-0.0.1-SNAPSHOT.jar. The .original file is the plain JAR Maven built before repackaging, and it lacks the dependencies and loader. When you run the executable JAR, the JVM starts the launcher named in Main-Class (JarLauncher). The launcher creates a class loader that can read the nested JARs in BOOT-INF/lib, then calls the main method of the class named in Start-Class, your @SpringBootApplication class.
Exercise
The packaged application.properties sets server.port=8081. How can you run the same JAR on port 9090 without rebuilding, and where must a -D option go on the command line?
Show answer
Pass --server.port=9090 after the JAR name, or set the SERVER_PORT environment variable, or use java -Dserver.port=9090 -jar demo-0.0.1-SNAPSHOT.jar. A -D option must come before -jar, because anything after the JAR name is passed to the application as arguments instead of to the JVM. All of these override the value packaged inside the JAR.