Sub topic 6
Compiling and running a native executable
This chapter assumes you can build a Spring Boot 3 project with Maven or Gradle, run it on Java 17 or later, and read a Spring Boot startup log. It also assumes you know what the closed-world assumption means from earlier chapters. Here you compile an application to a native executable, run it, and learn what the build does along the way.
What the native build does
A native build is a two-stage process. Spring's AOT (ahead-of-time) step runs first. It inspects your bean definitions and generates Java source, bytecode, and GraalVM hint files. The GraalVM native-image tool then analyzes the whole application, including those generated classes, and compiles everything reachable into one executable.
The result is a platform-specific binary. It contains your code, the used parts of Spring and your libraries, and a small runtime called Substrate VM. It does not need a JVM to run.
Prerequisites on your machine
To compile locally you need a GraalVM distribution for your Java version, with native-image available. Set JAVA_HOME to that distribution, or use a tool such as SDKMAN to switch to it. You also need a C toolchain, because native-image links the final binary with the system linker.
| Platform | Toolchain needed |
|---|---|
| Linux | `gcc`, `zlib` development headers (`zlib1g-dev` or equivalent) |
| macOS | Xcode command line tools |
| Windows | Visual Studio Build Tools with the Windows SDK, run from a developer prompt |
Check your setup before the first build:
java -version
native-image --versionIf native-image is not found, your JAVA_HOME probably points at a regular JDK instead of GraalVM.
Building with Maven
A project generated from Spring Initializr with the GraalVM Native Support dependency inherits from spring-boot-starter-parent. That parent defines a native profile. The profile applies the native-maven-plugin and runs Spring AOT processing for you.
./mvnw -Pnative native:compile
./target/demoThe first command compiles the application and writes the executable to target. The executable name comes from the project's artifactId, so a project named demo produces target/demo. The second command runs it directly. No java -jar is involved.
Building with Gradle
With Gradle, the org.graalvm.buildtools.native plugin adds the native tasks. When it is applied next to the Spring Boot plugin, Spring's AOT tasks are wired in automatically.
./gradlew nativeCompile
./gradlew bootBuildImagenativeCompile writes the executable to build/native/nativeCompile. The second command, bootBuildImage, builds a container image. The next chapter covers it in detail.
What happens during the build
The Maven and Gradle flows follow the same steps:
- The project is compiled to ordinary bytecode.
process-aotruns your application's startup logic in a special build-time mode. It evaluates conditions, registers bean definitions, and writes generated classes such asDemoApplication__BeanDefinitionsandDemoApplication__ApplicationContextInitializer.- Hint files are written under
META-INF/native-image. They describe the reflection, resources, and proxies that Spring knows it needs. native-imagestarts from themainmethod, follows every reachable call, and builds the list of classes, methods, and fields that stay in the binary.- Reachable code is compiled to machine code. Some classes are initialized at build time, and their state is stored in the image heap.
- The linker produces the executable.
You can inspect the generated output in target/spring-aot on Maven and build/generated/aotSources on Gradle. Reading these files is the fastest way to understand why a bean exists or does not exist in the native image.
Running and comparing
Run the executable and read the startup line:
./target/demoOn a JVM you see output like Started DemoApplication in 1.8 seconds (process running for 2.1). A native run of the same application typically reports a few dozen milliseconds. The exact numbers depend on the application and the machine, so measure your own application instead of relying on published figures.
Command-line arguments and environment variables work as they do on the JVM. Spring Boot property binding still applies:
SERVER_PORT=9090 ./target/demo --spring.application.name=demo-nativeJVM-specific options do not apply. You cannot pass -Xmx or -XX: flags to a native binary the same way. Heap settings are given in the form -Xmx512m as plain arguments to the executable, and the garbage collector is chosen at build time. The default collector is Serial GC.
Build cost and resource use
A native build is slow and memory-hungry compared with a normal build. Plan for several minutes for a small application, and for several gigabytes of RAM. Closed-world analysis visits all reachable code, which is the expensive part. For this reason, many teams build native images in CI and keep running the application on the JVM during daily development.
Useful options go into the build configuration. In Maven, set them on the plugin:
<plugin>
<groupId>org.graalvm.buildtools</groupId>
<artifactId>native-maven-plugin</artifactId>
<configuration>
<buildArgs>
<buildArg>-O1</buildArg>
<buildArg>--verbose</buildArg>
</buildArgs>
</configuration>
</plugin>The -O1 flag trades some runtime speed for a faster build, which suits local experiments. Use the default optimization level for release builds.
Differences you notice when running
- Platform. The executable runs only on the operating system and CPU architecture where it was built. A binary built on macOS will not run on Linux.
- No JIT warm-up. Performance is steady from the first request, but peak throughput is often lower than a JVM reaches after warm-up, because there is no runtime profiling.
- Smaller memory footprint. The process has no class loading, JIT compiler, or metadata for unused classes.
- No JVM tools.
jcmd, JFR-based workflows, and attaching a Java debugger work differently or not at all. Debug on the JVM first, then check native behavior. - Fixed configuration. Some decisions, such as bean conditions, were made during AOT processing. A later chapter covers this in full.
Common first-build problems
| Symptom | Likely cause |
|---|---|
| `native-image: command not found` | `JAVA_HOME` points at a plain JDK, not GraalVM |
| Linker or `cannot find -lz` errors | Missing C toolchain or `zlib` headers |
| Build killed without a Java stack trace | Not enough memory for the build |
| Executable starts, then fails with `ClassNotFoundException` or a missing resource | Reflection or resource metadata is missing |
| Property change has no effect at runtime | The value was evaluated at build time |
The last two rows are the most common once the build succeeds. Later chapters explain runtime hints, the tracing agent, and build-time evaluation.
Exercise
You built a Spring Boot project named inventory with ./mvnw -Pnative native:compile on a Linux CI server and copied target/inventory to a colleague's Mac. It does not start. Why, and what should the colleague do?
Show answer
A native executable is compiled for the operating system and CPU architecture where it was built, so a Linux binary cannot run on macOS. The colleague should either build the executable on the Mac with a local GraalVM installation, or run the application on the JVM. Alternatively, the Linux executable can run inside a Linux container.
Exercise
After the build finishes, where do you look to see which bean definitions Spring AOT generated, and in which build step were they created?
Show answer
Look in target/spring-aot for Maven or build/generated/aotSources for Gradle. The files are created by the Spring AOT processing step (process-aot), which runs before native-image starts its analysis.