Sub topic 6
Compiling and running a native executable
This chapter assumes you have Java 17 or later, a Spring Boot 3 project built with Maven or Gradle, and a GraalVM JDK installed. The previous chapter set up the project. Here you run the build and look at what it produces.
Check your toolchain
The build calls the native-image tool from your GraalVM installation. Confirm that your shell finds it before you start:
java -version
native-image --versionIf native-image is not found, JAVA_HOME probably points to a regular JDK. Point it at the GraalVM distribution and try again.
What the build does
A native build is longer than a normal one. It adds two stages after compilation.
- Maven or Gradle compiles your sources as usual.
- Spring AOT processing runs your application's configuration at build time. It generates bean definition code and runtime hints.
native-imageanalyses everything reachable frommain, including the generated code, and compiles it into one executable.
Building with Maven
A project that inherits from spring-boot-starter-parent already has a native profile. Activate it and run the native:compile goal:
./mvnw -Pnative native:compile
./target/demoThe executable is written to target. Its name comes from the project's artifactId, here demo. It starts without a JVM, so there is no java -jar in the command.
Building with Gradle
On Gradle, apply the org.graalvm.buildtools.native plugin next to the Spring Boot plugin. The task is nativeCompile:
./gradlew nativeCompile
./build/native/nativeCompile/demoGradle writes the executable to build/native/nativeCompile.
What to expect
The first run shows the main difference from the JVM. Startup time usually falls from seconds to tens of milliseconds. The Spring Boot startup line in the log reports the time. The application behaves the same, but it no longer needs a warm-up period.
The cost is on the build side:
- A build commonly takes several minutes, even for a small application.
- It can use several gigabytes of memory.
- The executable only runs on the operating system and CPU architecture you built it on. A binary built on macOS will not run on Linux.
Common first-build problems
| Symptom | Likely cause | What to do |
|---|---|---|
| `native-image: command not found` | `JAVA_HOME` points to a non-GraalVM JDK | Install GraalVM and update `JAVA_HOME` and `PATH` |
| Build is killed or runs out of memory | Not enough RAM for the analysis | Close other programs, or give the build more memory |
| Build succeeds but the app fails on start | Missing reflection or resource metadata | See the later chapters on hints and the tracing agent |
| Bean is missing in the native build | A condition was evaluated at build time | Check the properties used when the build ran |
The last two rows come from the closed-world assumption. The build decides what exists before the program runs. Later chapters cover both problems.
Exercise
You run ./mvnw -Pnative native:compile on a Linux CI server, and the build succeeds. A colleague copies target/demo to a Windows laptop and it does not start. Why, and how do you fix it?
Show answer
A native executable is compiled for one operating system and CPU architecture, so the Linux binary cannot run on Windows. The fix is to build on the target platform, or to build a container image and run it where a Linux container runtime is available.