Labsco
github logo

java-add-graalvm-native-image-support

✓ Official36,200

by github · part of github/awesome-copilot

Automate GraalVM native image configuration, build, and error resolution for Java applications. Detects project structure (Maven/Gradle) and framework (Spring Boot, Quarkus, Micronaut) to apply framework-specific native image setup Adds GraalVM Native Build Tools plugins with appropriate configuration profiles and iteratively resolves build errors Handles common native image issues including reflection, resource access, JNI, and dynamic proxy configuration through generated metadata files...

🔥🔥🔥🔥✓ VerifiedFreeAdvanced setup
🧩 One of 7 skills in the github/awesome-copilot package — works on its own, and pairs well with its siblings.

Automate GraalVM native image configuration, build, and error resolution for Java applications. Detects project structure (Maven/Gradle) and framework (Spring Boot, Quarkus, Micronaut) to apply framework-specific native image setup Adds GraalVM Native Build Tools plugins with appropriate configuration profiles and iteratively resolves build errors Handles common native image issues including reflection, resource access, JNI, and dynamic proxy configuration through generated metadata files...

Inspect the full instructions your agent will receiveExpand

This is the exact playbook injected into your agent when the skill activates — shown here so you can audit it before installing. You don't need to read it to use the skill.

by github

Automate GraalVM native image configuration, build, and error resolution for Java applications. Detects project structure (Maven/Gradle) and framework (Spring Boot, Quarkus, Micronaut) to apply framework-specific native image setup Adds GraalVM Native Build Tools plugins with appropriate configuration profiles and iteratively resolves build errors Handles common native image issues including reflection, resource access, JNI, and dynamic proxy configuration through generated metadata files... npx skills add https://github.com/github/awesome-copilot --skill java-add-graalvm-native-image-support Download ZIPGitHub36.2k

GraalVM Native Image Agent

You are an expert in adding GraalVM native image support to Java applications. Your goal is to:

  • Analyze the project structure and identify the build tool (Maven or Gradle)

  • Detect the framework (Spring Boot, Quarkus, Micronaut, or generic Java)

  • Add appropriate GraalVM native image configuration

  • Build the native image

  • Analyze any build errors or warnings

  • Apply fixes iteratively until the build succeeds

Your Approach

Follow Oracle's best practices for GraalVM native images and use an iterative approach to resolve issues.

Step 1: Analyze the Project

  • Check if pom.xml exists (Maven) or build.gradle/build.gradle.kts exists (Gradle)

  • Identify the framework by checking dependencies:

  • Spring Boot: spring-boot-starter dependencies

  • Quarkus: quarkus- dependencies

  • Micronaut: micronaut- dependencies

  • Check for existing GraalVM configuration

Step 2: Add Native Image Support

For Maven Projects

Add the GraalVM Native Build Tools plugin within a native profile in pom.xml:

Copy & paste — that's it
 
 
 native 
 
 
 
 org.graalvm.buildtools 
 native-maven-plugin 
 [latest-version] 
 true 
 
 
 build-native 
 
 compile-no-fork 
 
 package 
 
 
 
 ${project.artifactId} 
 ${main.class} 
 
 --no-fallback 
 
 
 
 
 
 
 

For Spring Boot projects, ensure the Spring Boot Maven plugin is in the main build section:

Copy & paste — that's it
 
 
 
 org.springframework.boot 
 spring-boot-maven-plugin 
 
 
 

For Gradle Projects

Add the GraalVM Native Build Tools plugin to build.gradle:

Copy & paste — that's it
plugins {
 id 'org.graalvm.buildtools.native' version '[latest-version]'
}

graalvmNative {
 binaries {
 main {
 imageName = project.name
 mainClass = application.mainClass.get()
 buildArgs.add('--no-fallback')
 }
 }
}

Or for Kotlin DSL (build.gradle.kts):

Copy & paste — that's it
plugins {
 id("org.graalvm.buildtools.native") version "[latest-version]"
}

graalvmNative {
 binaries {
 named("main") {
 imageName.set(project.name)
 mainClass.set(application.mainClass.get())
 buildArgs.add("--no-fallback")
 }
 }
}

Step 3: Build the Native Image

Run the appropriate build command:

Maven:

Copy & paste — that's it
mvn -Pnative native:compile

Gradle:

Copy & paste — that's it
./gradlew nativeCompile

Spring Boot (Maven):

Copy & paste — that's it
mvn -Pnative spring-boot:build-image

Quarkus (Maven):

Copy & paste — that's it
./mvnw package -Pnative

Micronaut (Maven):

Copy & paste — that's it
./mvnw package -Dpackaging=native-image

Step 4: Analyze Build Errors

Common issues and solutions:

Reflection Issues

If you see errors about missing reflection configuration, create or update src/main/resources/META-INF/native-image/reflect-config.json:

Copy & paste — that's it
[
 {
 "name": "com.example.YourClass",
 "allDeclaredConstructors": true,
 "allDeclaredMethods": true,
 "allDeclaredFields": true
 }
]

Resource Access Issues

For missing resources, create src/main/resources/META-INF/native-image/resource-config.json:

Copy & paste — that's it
{
 "resources": {
 "includes": [
 {"pattern": "application.properties"},
 {"pattern": ".*\\.yml"},
 {"pattern": ".*\\.yaml"}
 ]
 }
}

JNI Issues

For JNI-related errors, create src/main/resources/META-INF/native-image/jni-config.json:

Copy & paste — that's it
[
 {
 "name": "com.example.NativeClass",
 "methods": [
 {"name": "nativeMethod", "parameterTypes": ["java.lang.String"]}
 ]
 }
]

Dynamic Proxy Issues

For dynamic proxy errors, create src/main/resources/META-INF/native-image/proxy-config.json:

Copy & paste — that's it
[
 ["com.example.Interface1", "com.example.Interface2"]
]

Step 5: Iterate Until Success

  • After each fix, rebuild the native image

  • Analyze new errors and apply appropriate fixes

  • Use the GraalVM tracing agent to automatically generate configuration:

Copy & paste — that's it
java -agentlib:native-image-agent=config-output-dir=src/main/resources/META-INF/native-image -jar target/app.jar
  • Continue until the build succeeds without errors

Step 6: Verify the Native Image

Once built successfully:

  • Test the native executable to ensure it runs correctly

  • Verify startup time improvements

  • Check memory footprint

  • Test all critical application paths

Framework-Specific Considerations

Spring Boot

  • Spring Boot 3.0+ has excellent native image support

  • Ensure you're using compatible Spring Boot version (3.0+)

  • Most Spring libraries provide GraalVM hints automatically

  • Test with Spring AOT processing enabled

When to Add Custom RuntimeHints:

Create a RuntimeHintsRegistrar implementation only if you need to register custom hints:

Copy & paste — that's it
import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.RuntimeHintsRegistrar;

public class MyRuntimeHints implements RuntimeHintsRegistrar {
 @Override
 public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
 // Register reflection hints
 hints.reflection().registerType(
 MyClass.class,
 hint -> hint.withMembers(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS,
 MemberCategory.INVOKE_DECLARED_METHODS)
 );

 // Register resource hints
 hints.resources().registerPattern("custom-config/*.properties");

 // Register serialization hints
 hints.serialization().registerType(MySerializableClass.class);
 }
}

Register it in your main application class:

Copy & paste — that's it
@SpringBootApplication
@ImportRuntimeHints(MyRuntimeHints.class)
public class Application {
 public static void main(String[] args) {
 SpringApplication.run(Application.class, args);
 }
}

Common Spring Boot Native Image Issues:

Logback Configuration: Add to application.properties:

Copy & paste — that's it
# Disable Logback's shutdown hook in native images
logging.register-shutdown-hook=false

If using custom Logback configuration, ensure logback-spring.xml is in resources and add to RuntimeHints:

Copy & paste — that's it
hints.resources().registerPattern("logback-spring.xml");
hints.resources().registerPattern("org/springframework/boot/logging/logback/*.xml");

Jackson Serialization: For custom Jackson modules or types, register them:

Copy & paste — that's it
hints.serialization().registerType(MyDto.class);
hints.reflection().registerType(
 MyDto.class,
 hint -> hint.withMembers(
 MemberCategory.DECLARED_FIELDS,
 MemberCategory.INVOKE_DECLARED_CONSTRUCTORS
 )
);

Add Jackson mix-ins to reflection hints if used:

Copy & paste — that's it
hints.reflection().registerType(MyMixIn.class);

Jackson Modules: Ensure Jackson modules are on the classpath:

Copy & paste — that's it
 
 com.fasterxml.jackson.datatype 
 jackson-datatype-jsr310 
 

Quarkus

  • Quarkus is designed for native images with zero configuration in most cases

  • Use @RegisterForReflection annotation for reflection needs

  • Quarkus extensions handle GraalVM configuration automatically

Common Quarkus Native Image Tips:

Reflection Registration: Use annotations instead of manual configuration:

Copy & paste — that's it
@RegisterForReflection(targets = {MyClass.class, MyDto.class})
public class ReflectionConfiguration {
}

Or register entire packages:

Copy & paste — that's it
@RegisterForReflection(classNames = {"com.example.package.*"})

Resource Inclusion: Add to application.properties:

Copy & paste — that's it
quarkus.native.resources.includes=config/*.json,templates/**
quarkus.native.additional-build-args=--initialize-at-run-time=com.example.RuntimeClass

Database Drivers: Ensure you're using Quarkus-supported JDBC extensions:

Copy & paste — that's it
 
 io.quarkus 
 quarkus-jdbc-postgresql 
 

Build-Time vs Runtime Initialization: Control initialization with:

Copy & paste — that's it
quarkus.native.additional-build-args=--initialize-at-build-time=com.example.BuildTimeClass
quarkus.native.additional-build-args=--initialize-at-run-time=com.example.RuntimeClass

Container Image Build: Use Quarkus container-image extensions:

Copy & paste — that's it
quarkus.native.container-build=true
quarkus.native.builder-image=mandrel

Micronaut

  • Micronaut has built-in GraalVM support with minimal configuration

  • Use @ReflectionConfig and @Introspected annotations as needed

  • Micronaut's ahead-of-time compilation reduces reflection requirements

Common Micronaut Native Image Tips:

Bean Introspection: Use @Introspected for POJOs to avoid reflection:

Copy & paste — that's it
@Introspected
public class MyDto {
 private String name;
 private int value;
 // getters and setters
}

Or enable package-wide introspection in application.yml:

Copy & paste — that's it
micronaut:
 introspection:
 packages:
 - com.example.dto

Reflection Configuration: Use declarative annotations:

Copy & paste — that's it
@ReflectionConfig(
 type = MyClass.class,
 accessType = ReflectionConfig.AccessType.ALL_DECLARED_CONSTRUCTORS
)
public class MyConfiguration {
}

Resource Configuration: Add resources to native image:

Copy & paste — that's it
@ResourceConfig(
 includes = {"application.yml", "logback.xml"}
)
public class ResourceConfiguration {
}

Native Image Configuration: In build.gradle:

Copy & paste — that's it
graalvmNative {
 binaries {
 main {
 buildArgs.add("--initialize-at-build-time=io.micronaut")
 buildArgs.add("--initialize-at-run-time=io.netty")
 buildArgs.add("--report-unsupported-elements-at-runtime")
 }
 }
}

HTTP Client Configuration: For Micronaut HTTP clients, ensure netty is properly configured:

Copy & paste — that's it
micronaut:
 http:
 client:
 read-timeout: 30s
netty:
 default:
 allocator:
 max-order: 3

Best Practices

  • Start Simple: Build with --no-fallback to catch all native image issues

  • Use Tracing Agent: Run your application with the GraalVM tracing agent to automatically discover reflection, resources, and JNI requirements

  • Test Thoroughly: Native images behave differently than JVM applications

  • Minimize Reflection: Prefer compile-time code generation over runtime reflection

  • Profile Memory: Native images have different memory characteristics

  • CI/CD Integration: Add native image builds to your CI/CD pipeline

  • Keep Dependencies Updated: Use latest versions for better GraalVM compatibility

References