GraalVM native image
What you will learn
GraalVM Native Image analyzes a known application and dependencies, then builds an executable for a target OS and architecture. Its runtime still provides Java features such as garbage collection; avoiding a separate JVM launch does not remove memory, security, or compatibility requirements.
- Describe the closed-world build pipeline.
- Identify a portable functional test before native compilation.
- Explain vendor, version and build-tool selection.
Plain Java examples: save as Main.java in a fresh directory, then run javac --release 21 Main.java and java Main. Standalone Java 21 functional examples. GraalVM 25, Spring AOT and HotSpot 25 AOT cache recipes are source-reviewed, not executed. Companion AppCDS evidence is separate.
A finite dispatch table
public class Main { static String label(String key) { return switch(key) { case "book" -> "Book"; case "pen" -> "Pen"; default -> throw new IllegalArgumentException(); }; } public static void main(String[] args) { System.out.println(label("book")); System.out.println(label("pen")); }}Expected output:
BookPenA fixed switch exposes reachable branches. The user input remains runtime data.
Build and run
The companion project uses a tiny JAR with a known entrypoint. On an approved GraalVM 25 environment, compile with the selected JDK and run native-image --no-fallback -jar catalog.jar catalog from the project build directory. Test the executable separately from java -jar catalog.jar. Check target OS/architecture, C toolchain, libc and dependencies, supported features and patch status. No compiler is installed here; the command is a reviewed recipe only.
Distribution and frameworks
GraalVM Community Edition uses GPLv2 with Classpath Exception; Oracle GraalVM uses GFTC terms for its current line. Verify exact distribution, version, redistribution terms, third-party notices and security patch cadence. Spring Boot AOT generates initialization code and runtime hints; Quarkus offers build-time augmentation and native tooling; Micronaut emphasizes compile-time DI. Framework choices require testing actual dependency versions. Helidon is another optional lane, not required here.
Keep domain behavior independent of packaging
public class Main { static int total(int units,int cents) { if(units<0 || cents<0) throw new IllegalArgumentException(); return Math.multiplyExact(units,cents); } public static void main(String[] args) { System.out.println(total(3, 120)); }}Expected output:
360The same test belongs in JVM and native suites.
Do not confuse target metadata with portability
public class Main { record Target(String os,String arch) {} public static void main(String[] args) { var target=new Target("linux","amd64"); System.out.println(target.equals(new Target("linux","amd64"))); System.out.println(target.equals(new Target("linux","arm64"))); }}Expected output:
truefalseA model of artifact selection, not an OS compatibility detector.
Common mistakes
Do not treat a JVM run as a native run. Do not enable fallback or ignore missing registrations simply to get an executable. Do not assume binaries built on one OS run on another.
Practice
Implement total with nonnegative quantities and overflow checking; print 720 for six items at 120 cents.
Expected output:
720Compare with the manual review criteria.
Further reading: Native dynamic · Native licensing · Spring native · Quarkus. Independent editorial review remains pending.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
GraalVM native image — knowledge check
AOT and deployment reasoning.
What is the closed-world assumption?
What does a successful JVM test establish?
What is a target constraint?
Which license must you review?
What does Spring AOT generate?