Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 

README.md

Build a Layered Native Executable Using the native-image Tool

This is a HelloWorld Java example, referenced from Getting Started with Native Image, using the GraalVM Native Image Layers feature.

Prerequisites

  • 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.

Environment Setup

Point your JAVA_HOME to the GraalVM distribution.

export JAVA_HOME=/path/to/graalvm/ea/build

Run the Application from a Class File

  1. 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.

  2. Run the application from a class file:

    java -cp ./build com.example.HelloWorld

    It outputs the message "Hello, Native World!".

Run the Application from a JAR File

  1. Create a JAR for the application, running the following command:
    jar --create --file HelloWorld.jar --main-class com.example.HelloWorld -C build .
  2. Run the JAR file:
    java -jar HelloWorld.jar

Run the Application as a Native Executable

  1. 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.

  2. 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.jar

    The executable called HelloWorld will be created in the working directory.

  3. Execute it:

    ./HelloWorld