Sub topic 2
The executable JAR and running it with java -jar
This chapter covers what the Spring Boot plugin puts inside the JAR it builds, how java -jar starts it, and the errors you are most likely to meet. It assumes you can build a Spring Boot project with Maven or Gradle, have a working JDK, and are comfortable on the command line. The previous chapter covered the build plugin itself, so this chapter focuses on the artifact it produces.
Why a plain JAR is not enough
A normal JAR is a ZIP archive of compiled classes and resources. The JDK can run it with java -jar, but it cannot load other JARs nested inside it. A typical Spring Boot application depends on dozens of libraries. Without extra help, you would have to do one of two things:
- Put every dependency on the classpath yourself and call the main class by name.
- Unpack all dependencies and merge them into one large "fat" JAR, which can cause file name clashes and loses track of where each class came from.
Spring Boot takes a third route. The plugin builds an executable JAR (sometimes called a "fat JAR" or "uber JAR") that keeps dependencies as separate nested JARs. A small loader included in the archive knows how to read them. You run the result with one command and nothing else to install.
Building the JAR
The plugin hooks into the normal build lifecycle. In Maven, the repackage goal runs during package. In Gradle, the bootJar task produces the executable archive.
# Maven
./mvnw clean package
# Gradle
./gradlew clean bootJar
# Run the result
java -jar target/myapp-1.0.0.jar --spring.profiles.active=prodGradle writes its output to build/libs/, not target/. Adjust the path for your build tool.
The --spring.profiles.active=prod argument comes after the JAR name, so it goes to your application and not to the JVM. Arguments placed before -jar are JVM options, such as -Xmx512m. Mixing up the order is a common source of confusion.
java -Xmx512m -jar target/myapp-1.0.0.jar --server.port=9090Here -Xmx512m configures the JVM heap, and --server.port=9090 configures Spring Boot.
What is inside the archive
You can inspect the layout with the jar tool that ships with the JDK:
jar tf target/myapp-1.0.0.jar | head -n 20The structure looks roughly like this:
| Path in the JAR | Contents |
|---|---|
| `META-INF/MANIFEST.MF` | Manifest naming the launcher and your main 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/` | Dependency JARs, stored uncompressed |
| `BOOT-INF/classpath.idx` | Ordered list of the dependency JARs |
| `BOOT-INF/layers.idx` | Layer definitions used for container images |
The two index files are generated by the plugin. classpath.idx keeps the dependency order stable, so the classpath is the same on every start. layers.idx groups files by how often they change, which matters when you build container images in a later chapter.
How java -jar starts the application
The JVM does not start your main method directly. It reads the manifest and follows two entries:
Main-Classpoints to the Spring Boot launcher, for exampleorg.springframework.boot.loader.launch.JarLauncher. The package name isorg.springframework.boot.loaderin Boot 2.x and 3.0 to 3.1, andorg.springframework.boot.loader.launchin later versions.Start-Classnames your application class, the one annotated with@SpringBootApplication.
You can read both from the manifest:
unzip -p target/myapp-1.0.0.jar META-INF/MANIFEST.MFThe sequence at start-up is as follows.
The launcher is why nested JARs work. It creates a class loader that can read entries from JARs inside the outer JAR, then calls your main method through that loader.
Why dependency JARs are stored uncompressed
Entries in BOOT-INF/lib/ are stored without compression. The loader reads them directly from the outer file, and uncompressed entries can be accessed without unpacking them first. The cost is a larger file. The gain is faster start-up and no temporary files.
This has one practical effect: do not re-zip or re-compress the JAR with a general-purpose tool. If you do, the entries may become compressed, and the loader can fail. Use the plugin to rebuild it.
Checking that the JAR runs
Before you hand the JAR to anyone else, run it on a clean machine or in a clean directory. This catches missing files that only worked in your IDE.
java -jar target/myapp-1.0.0.jarIn the log output, look for the line that reports the port and the time taken:
Tomcat started on port 8080 (http) with context path '/'
Started Application in 3.2 seconds (process running for 3.6)If the application starts and responds to a request, the artifact works. Press Ctrl+C to stop it. This sends SIGTERM to the process, and the JVM runs its shutdown hooks. Spring registers a hook that closes the application context. The chapter on graceful shutdown covers what that closing does to in-flight requests.
Choosing the Java version
The JAR contains bytecode compiled for a specific Java version, set by java.version in Maven or the toolchain in Gradle. The JVM that runs it must be the same version or newer. If it is older, you get an error like this:
Exception in thread "main" java.lang.UnsupportedClassVersionError: com/example/Application has been compiled by a more recent version of the Java Runtime (class file version 65.0), this version of the Java Runtime only recognizes class file versions up to 61.0Class file version 65 is Java 21, and 61 is Java 17. The fix is to run with a newer JDK or to build for an older target. Check which java is first on the PATH with java -version, since servers often carry an old JDK from an earlier install.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `no main manifest attribute, in app.jar` | You ran the plain JAR, not the repackaged one, or the plugin is missing | Add the Spring Boot plugin and run the JAR it built |
| `UnsupportedClassVersionError` | The runtime JDK is older than the build target | Install a matching JDK or lower the target |
| `Port 8080 was already in use` | Another process holds the port | Stop it or pass `--server.port=` with a free port |
| `ClassNotFoundException` for a dependency | The JAR was re-zipped or modified | Rebuild with the plugin |
| Application ignores a command-line option | The option was placed before `-jar` | Put application arguments after the JAR name |
The first error deserves a closer look. In Maven, the build can produce two files when the plugin runs: the executable JAR and a .jar.original file that holds the plain, un-repackaged archive. In Gradle, bootJar produces the executable file, and the ordinary jar task also produces a second file with the suffix -plain. If you copy the wrong file, you get the missing manifest error.
ls target/*.jar*
# myapp-1.0.0.jar
# myapp-1.0.0.jar.originalThe file without .original is the one to run.
Executable JAR or exploded directory
You can run the application from an unpacked directory, which is useful for debugging the layout and is the basis of the layered container images later in the tutorial.
mkdir -p unpacked
cd unpacked
jar xf ../target/myapp-1.0.0.jar
java org.springframework.boot.loader.launch.JarLauncherUse the loader class name that matches your Boot version. For most deployments, keep the single file. It is easier to copy, version, and checksum, and it behaves the same everywhere.
Decision criteria
The executable JAR is a good default. Use it when:
- You want one file that holds your code and all dependencies.
- The target has a JDK or JRE installed, or you will provide one in a container.
- You run one application per process and want it to embed its own web server.
Consider a different packaging when you must deploy into an existing servlet container (see the WAR chapter), when you want a smaller, faster-starting binary (see the native image chapter), or when you need cache-friendly container layers (see the container chapter).
Exercise
You run java -jar app.jar -Xmx256m --server.port=9090 and the heap setting has no effect. Why, and how do you fix the command?
Show answer
Everything after the JAR name is passed to the application as arguments, so -Xmx256m is not read as a JVM option. JVM options must come before -jar. The fixed command is java -Xmx256m -jar app.jar --server.port=9090.
Exercise
A teammate copies build/libs/myapp-1.0.0-plain.jar to a server and gets "no main manifest attribute". What happened, and which file should they copy?
Show answer
The -plain file is the ordinary JAR produced by Gradle's jar task. It contains only your classes and has no Spring Boot launcher or nested dependencies. They should copy the executable file produced by bootJar, build/libs/myapp-1.0.0.jar.