Sub topic 1
JIT on the JVM versus Ahead-of-Time Native Executables
Prerequisites
Assume Java 17 or later, basic Spring Boot knowledge, and experience building projects with Maven or Gradle. You should also know that reflection discovers classes or methods at runtime.
A Java application normally runs on the Java Virtual Machine (JVM). The JVM starts by interpreting bytecode and then uses a just-in-time (JIT) compiler to optimize frequently executed code while the application runs.
GraalVM Native Image uses ahead-of-time (AOT) compilation. It analyzes the application during the build and produces a platform-specific executable. The executable starts without first starting a JVM or warming up a JIT compiler.
| Execution model | Main work time | Typical benefit |
|---|---|---|
| JVM with JIT | During startup and runtime | Runtime optimization and broad Java compatibility |
| Native executable | Mostly during the build | Fast startup and lower memory use |
Native compilation increases build time and can expose code that depends on runtime discovery.
The Closed-World Assumption
Native Image uses a closed-world assumption: the build must determine every class, method, resource, and proxy that the executable might need. Code reached through ordinary calls is easy to analyze. Reflection, dynamically loaded classes, resource files, and generated proxies may be invisible to static analysis.
Missing reachability metadata can therefore cause a native build failure or a runtime failure such as ClassNotFoundException. The JVM often handles the same code because it can inspect the classpath at runtime.
What Native Image Produces
The native-image tool combines application classes, selected library code, configuration, and a runtime substrate into one executable. It removes unreachable code and performs optimization during the build. The result is tied to the target operating system and CPU architecture.
How Spring AOT Prepares Your Application
Spring AOT processes the application context during the build. It generates code for bean registration and records hints for reflection, resources, and proxies. Conditions such as @ConditionalOnProperty can be evaluated at build time, so changing the property later cannot create a bean that was excluded from the executable.
Setting Up a Spring Boot Project
For Maven, use Spring Boot’s native profile. For Gradle, apply the GraalVM Native Build Tools plugin. A Maven project using spring-boot-starter-parent can build and run a native executable with:
./mvnw -Pnative native:compile
./target/demoYou can also build a native container image with Cloud Native Buildpacks:
./mvnw -Pnative spring-boot:build-imageThe Gradle equivalents are:
./gradlew nativeCompile
./gradlew bootBuildImageExercise
Why can an application that works on the JVM fail after native compilation?
Show answer
Native Image analyzes a closed world at build time, so runtime reflection, resources, or proxies may need metadata that the JVM does not require.