The GraalVM SDK Native Image API allows to customize the native image generation, i.e., the ahead-of-time compilation of Java code to standalone executables
npx @tessl/cli install tessl/maven-org-graalvm-sdk--nativeimage@24.2.0The GraalVM SDK Native Image API enables customization of native image generation - the ahead-of-time compilation of Java code to standalone executables. It provides comprehensive control over the compilation process, C interoperability, memory management, and runtime behavior customization.
pom.xml:<dependency>
<groupId>org.graalvm.sdk</groupId>
<artifactId>nativeimage</artifactId>
<version>24.2.1</version>
</dependency>import org.graalvm.nativeimage.*;
import org.graalvm.nativeimage.hosted.*;
import org.graalvm.nativeimage.c.*;
import org.graalvm.nativeimage.c.function.*;
import org.graalvm.nativeimage.c.struct.*;
import org.graalvm.nativeimage.c.type.*;// Check execution context
if (ImageInfo.inImageBuildtimeCode()) {
// This code runs during native image generation
System.out.println("Building native image...");
} else if (ImageInfo.inImageRuntimeCode()) {
// This code runs in the native executable
System.out.println("Running in native image");
}
// Platform-specific code
if (Platform.includedIn(Platform.LINUX.class)) {
// Linux-specific implementation
}
// Memory management
try (PinnedObject pinnedArray = PinnedObject.create(byteArray)) {
CCharPointer pointer = pinnedArray.addressOfArrayElement(0);
// Use pointer for native operations
}
// Isolate management
IsolateThread currentThread = CurrentIsolate.getCurrentThread();
Isolate isolate = CurrentIsolate.getIsolate();The GraalVM Native Image SDK is organized into several key architectural components:
@Platforms annotation for platform-specific codeCore runtime functionality including isolate management, memory allocation, threading support, and platform abstraction. Essential for native image execution and runtime behavior.
// Context and Information
class ImageInfo {
static boolean inImageBuildtimeCode();
static boolean inImageRuntimeCode();
static boolean inImageCode();
static boolean isExecutable();
static boolean isSharedLibrary();
}
// Platform Support
interface Platform {}
@interface Platforms {
Class<? extends Platform>[] value();
}
// Isolate Management
interface Isolate extends PointerBase {}
interface IsolateThread extends PointerBase {}
class CurrentIsolate {
static IsolateThread getCurrentThread();
static Isolate getIsolate();
}Comprehensive C interoperability framework providing type-safe integration between Java and C code. Enables direct function calls, struct mapping, and memory operations without JNI overhead.
// Function Interop
@interface CFunction {
String value() default "";
}
@interface CEntryPoint {
String name() default "";
}
// Struct Mapping
@interface CStruct {
String value() default "";
}
@interface CField {
String value() default "";
}
// Type System
interface VoidPointer extends PointerBase {}
interface CCharPointer extends PointerBase {}
interface WordPointer extends PointerBase {}Build-time APIs for customizing native image generation, including feature registration, reflection configuration, and lifecycle management. Used during the compilation phase to control what gets included in the final executable.
// Feature System
interface Feature {
boolean isInConfiguration(IsInConfigurationAccess access);
void beforeAnalysis(BeforeAnalysisAccess access);
void duringAnalysis(DuringAnalysisAccess access);
void afterAnalysis(AfterAnalysisAccess access);
void beforeCompilation(BeforeCompilationAccess access);
void afterHeapLayout(AfterHeapLayoutAccess access);
}
// Configuration APIs
class RuntimeReflection {
static void register(Class<?>... classes);
static void register(Method... methods);
static void register(Field... fields);
}
class RuntimeJNIAccess {
static void register(Class<?>... classes);
static void register(Method... methods);
}Uses ImageSingletons for compile-time constant lookup and configuration sharing.
Extensive use of annotations (@CFunction, @CStruct, @Platforms) for declarative configuration.
Most APIs use interfaces for flexibility and extensibility.
Feature system provides comprehensive build-time control through well-defined phases.
Word types and pointer abstractions ensure memory safety while maintaining performance.
This API enables developers to create high-performance native executables from Java applications with full control over compilation, memory management, and platform integration while maintaining Java's type safety and development experience.