Sub topic 13
Packaging an Executable JAR
This chapter assumes you have a running Spring Boot project built with Maven and the Maven wrapper (mvnw). It also assumes you know what a JAR file is and have a JDK on your path. You will build a single file that contains your app and runs with java -jar.
Why a Different Kind of JAR
A normal JAR holds only your compiled classes. To run it, you would also need to put every dependency on the classpath yourself. A Spring Boot executable JAR, often called a fat JAR, bundles your classes, all dependency JARs, and a small launcher. The embedded server from the web starter is included, so the file needs nothing but a Java runtime.
The Spring Boot Maven plugin creates this file. The project you generated with Spring Initializr already declares it in pom.xml, so no extra setup is needed.
Building the JAR
The package phase compiles the code, runs the tests, and writes the JAR to target/.
./mvnw clean package
java -jar target/demo-0.0.1-SNAPSHOT.jar
curl http://localhost:8080/helloThe file name comes from the artifactId and version in your pom.xml. With the defaults, that gives demo-0.0.1-SNAPSHOT.jar. The clean goal removes old output first, so stale files cannot end up in the build.
If your application.properties sets server.port=8081, call port 8081 instead.
What Is Inside
The plugin runs a step called repackaging. It takes the plain JAR that Maven built and rewrites it into a layout like this:
BOOT-INF/classesholds your compiled classes and resources, includingapplication.properties.BOOT-INF/libholds the dependency JARs, each kept as a nested JAR.org/springframework/boot/loaderholds the launcher classes.META-INF/MANIFEST.MFnames the launcher asMain-Classand your application class asStart-Class.
A standard JVM cannot read nested JARs on its own. When you run java -jar, the JVM starts the launcher, the launcher builds a classpath from BOOT-INF, and it then calls your main method. You can list the contents to check:
jar tf target/demo-0.0.1-SNAPSHOT.jar | head -20Overriding Configuration at Run Time
The JAR is a fixed artifact, but you can still change its settings without rebuilding. Command-line arguments take priority over the application.properties file packed inside.
java -jar target/demo-0.0.1-SNAPSHOT.jar --server.port=9090This lets the same build run in development, test, and production with different settings.
Common Errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `no main manifest attribute, in app.jar` | The JAR was not repackaged, for example because the plugin is missing from `pom.xml`. | Add `spring-boot-maven-plugin` to the `build` section and rebuild. |
| `Unable to access jarfile` | Wrong path or file name. | Check `target/` with `ls` and match the name exactly. |
| `Port 8080 was already in use` | Another process is using the port. | Stop it, or pass `--server.port=` with a free port. |
| `UnsupportedClassVersionError` | The runtime Java version is older than the one used to build. | Run with a JDK at least as new as the build target. |
Development Run vs. JAR
./mvnw spring-boot:run is convenient while you code. Use the JAR when you want to test what you will actually deploy. Packaged runs catch problems that the development run hides, such as resources that exist in src but are missing from the build.
Exercise
You run java -jar target/demo-0.0.1-SNAPSHOT.jar and get no main manifest attribute. What is the most likely cause, and how do you fix it?
Show answer
The JAR was not repackaged by the Spring Boot Maven plugin, so it has no launcher or Main-Class entry. Make sure spring-boot-maven-plugin is in the build plugins of pom.xml, then run ./mvnw clean package again.
Exercise
How can you run the packaged JAR on port 9090 without rebuilding it?
Show answer
Pass the property as a command-line argument: java -jar target/demo-0.0.1-SNAPSHOT.jar --server.port=9090. Command-line arguments override application.properties.