If you want to build Java apps that start instantly, use less memory, and are perfect for microservices or cloud, then this guide is for you. With Micronaut and GraalVM Native Image Compilation, you can compile your Java application into a small and fast executable.
Micronaut is a lightweight Java framework made for speed. GraalVM turns your Micronaut app into a native binaryโno JVM needed at runtime. In this post, weโll show you how to build, configure, and compile a Micronaut app with GraalVM using Maven, and the package com.kscodes.micronaut.graalvm
.

๐งฐ What is GraalVM Native Image?
GraalVM Native Image converts a Java app into a standalone native executable. That means:
- No JVM is required to run it.
- Startup is super-fast (great for serverless).
- Uses less memory.
Benefits:
โ
Starts in milliseconds
โ
Uses less RAM
โ
Works great in Docker containers or cloud
โ๏ธ Prerequisites
- Java 17+
- GraalVM installed and set as default JDK
- Maven 3.x
- Install the native-image tool:
1 2 3 4 |
gu install native-image |
๐ฆ Creating a Micronaut App (Maven)
You can use Micronaut Launch or the CLI to generate the app.
1 2 3 4 5 6 7 |
mn create-app com.kscodes.micronaut.graalvm.nativeapp \ --features graalvm \ --build maven \ --jdk 17 |
This creates a base project with the necessary setup for GraalVM native image compilation.
๐ Maven Configuration (pom.xml
)
โ Add Dependencies:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<dependency> <groupId>io.micronaut</groupId> <artifactId>micronaut-inject</artifactId> </dependency> <dependency> <groupId>io.micronaut</groupId> <artifactId>micronaut-runtime</artifactId> </dependency> <dependency> <groupId>io.micronaut.graal</groupId> <artifactId>micronaut-graal</artifactId> <scope>provided</scope> </dependency> |
โ Add GraalVM Plugin:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<plugin> <groupId>org.graalvm.buildtools</groupId> <artifactId>native-maven-plugin</artifactId> <version>0.9.28</version> <executions> <execution> <goals> <goal>compile-no-fork</goal> </goals> </execution> </executions> </plugin> |
This plugin builds your Java code into a native binary using GraalVM.
๐จโ๐ป Example Code
Letโs create a simple REST controller.
File: com.kscodes.micronaut.graalvm.HelloController.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
package com.kscodes.micronaut.graalvm; import io.micronaut.http.annotation.*; @Controller("/hello") public class HelloController { @Get public String greet() { return "Hello from GraalVM Native Micronaut App!"; } } |
๐ Build the Project
Step 1: Package the Application
1 2 3 4 |
./mvnw clean package |
Step 2: Build Native Image
1 2 3 4 |
./mvnw package -Dpackaging=native-image |
After some processing, the executable binary will be available at:
1 2 3 4 |
target/native-image/nativeapp |
Run it like a normal command-line app:
1 2 3 4 |
./target/native-image/nativeapp |
๐ Test the Endpoint
Check the running server:
1 2 3 4 |
curl http://localhost:8080/hello |
Output
1 2 3 4 |
Hello from GraalVM Native Micronaut App! |
You’ll notice how quickly the app starts and responds.
๐ง Key Notes
- GraalVM doesnโt support dynamic reflection unless configured.
- Native images are platform-specific (build for Linux on Linux, etc.).
- Use GraalVM Reachability Metadata for third-party libraries.
๐ External Resources
โ Conclusion
Using Micronaut and GraalVM Native Image Compilation, you can turn your Java application into a fast, efficient, and portable binary. This is great for microservices, CLI tools, or cloud-native apps. With the help of Maven and minimal configuration, the native image process becomes seamless.
The com.kscodes.micronaut.graalvm
package structure shown in this tutorial provides a clean template to begin your journey into native Java with Micronaut.