Sub topic 2
The executable JAR and running it with java -jar
This chapter assumes you can build a Spring Boot project with Maven or Gradle and run java from a terminal. The previous chapter covered the build plugin. This one covers what the plugin produces and how to run it.
What the plugin produces
A normal JAR holds only your compiled classes. Your application also needs Spring, Tomcat, and its other dependencies, and a plain JAR does not contain them. The Spring Boot plugin repackages the build output into an executable JAR, often called a fat JAR. It contains your classes, all dependency JARs, and a small launcher.
Java cannot load JARs nested inside a JAR by default. Spring Boot solves this with its own launcher class, which is named in the manifest as Main-Class. The launcher builds a class loader that reads the nested JARs, then calls the Start-Class entry from the manifest, which is your @SpringBootApplication class.
Building the JAR
Run the build from the project root. The clean step removes old output so you do not package stale files.
# Maven
./mvnw clean package
# Gradle
./gradlew clean bootJarMaven writes the JAR to target/. Gradle writes it to build/libs/. Gradle also builds a plain JAR when the java plugin is applied. Its name ends in -plain.jar, and you should not deploy it.
You can look inside the result:
jar tf target/myapp-1.0.0.jar | head -20Your classes appear under BOOT-INF/classes, the dependencies under BOOT-INF/lib, and the launcher under org/springframework/boot/loader.
Running it
java -jar target/myapp-1.0.0.jar --spring.profiles.active=prodArguments placed after the JAR name go to your application. Spring Boot reads those that start with -- as configuration properties. Here the prod profile is activated. Arguments placed before -jar go to the JVM itself, for example java -Xmx512m -jar app.jar.
The process stays in the foreground and stops when you press Ctrl+C. That sends SIGTERM, which Spring handles as a normal shutdown. A later chapter covers keeping the process running in the background.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `no main manifest attribute, in app.jar` | You ran the plain JAR, or the plugin did not repackage | Run the `bootJar` or `repackage` output. Check that the plugin is applied |
| `UnsupportedClassVersionError` | The runtime JDK is older than the one used to compile | Run with the same or a newer JDK. Check with `java -version` |
| `Port 8080 was already in use` | Another process holds the port | Stop it, or pass `--server.port=8081` |
| `ClassNotFoundException` for a library | The dependency has `provided` or `compileOnly` scope | Move it to `implementation` or compile scope |
Why this artifact matters
The same JAR file is what you copy to a server, put in a container, or hand to a CI/CD pipeline. It contains no environment-specific settings. The next chapters keep it that way by supplying configuration from outside.
Exercise
You run java -jar app.jar -Xmx256m and the JVM heap does not change. Why, and how do you fix it?
Show answer
Arguments after the JAR name go to the application, not the JVM, so the JVM never sees -Xmx256m. Move the flag before -jar: java -Xmx256m -jar app.jar.
Exercise
After a Gradle build, build/libs has two JAR files. Which one do you run, and how do you recognize it?
Show answer
Run the one built by bootJar, which is the executable JAR. The other ends in -plain.jar and contains only your classes, so running it fails with no main manifest attribute.