This is a HelloWorld Java example, referenced from Getting Started with Native Image, using the GraalVM Native Image Layers feature.
- Linux x64
- Latest GraalVM 25.1 EA build (with Native Image Layers support)
Native Image Layers is an experimental feature. For the best experience, use the latest GraalVM Early Access Build.
Point your JAVA_HOME to the GraalVM distribution.
export JAVA_HOME=/path/to/graalvm/ea/build-
Compile the application by running the following command:
javac -d build src/com/example/HelloWorld.java
This command generates the HelloWorld.class file in the build/com/example directory.
-
Run the application from a class file:
java -cp ./build com.example.HelloWorld
It outputs the message "Hello, Native World!".
- Create a JAR for the application, running the following command:
jar --create --file HelloWorld.jar --main-class com.example.HelloWorld -C build . - Run the JAR file:
java -jar HelloWorld.jar
-
Create a base layer that contains
java.base:mkdir -p native-build native-image -H:LayerCreate=baselayer.nil,module=java.base -o libjavabaselayer -H:Path=./native-build
This command creates:
- base-layer.nil, which is a build-time dependency for the application build.
- libjavabaselayer.so, which is a runtime dependency for the application layer.
For more details consult the Native Image Layers documentation.
-
Create a native executable from a JAR file using the base layer:
native-image -H:LayerUse=native-build/libjavabaselayer.nil -H:LinkerRPath="\$ORIGIN/native-build" -jar HelloWorld.jarThe executable called
HelloWorldwill be created in the working directory. -
Execute it:
./HelloWorld