The demo is a Java program that simulates the traditional fortune
Unix program (for more information, see fortune). The data for the fortune phrases is provided by YourFortune.
The Fortune demo is comprised of three sub-projects:
- The fortune-maven project uses the Maven plugin for GraalVM Native Image building.
- The fortune-gradle project uses the Gradle plugin for GraalVM Native Image building.
- The staticfortune project demonstrates how to build a statically linked native executable using the Maven plugin for GraalVM Native Image building.
This demo is a little more complicated than HelloWorld, and requires pre-configuration before building a native executable. The Maven and Gradle plugins for Native Image building can generate the required configuration for you by injecting the Java agent at package time.
The plugin will also gather the diagnostic data at build time and write it to a dump file in the target
directory.
-
Download and install the latest GraalVM for JDK 21 using SDKMAN!.
sdk install java 21.0.3-graal
-
Download or clone GraalVM demos repository:
git clone https://github.com/graalvm/graalvm-demos
-
Change to the fortune-demo/fortune-maven directory:
cd fortune-demo/fortune-maven
-
Build the project:
mvn clean package
-
Run your application with the agent, on the JVM:
mvn -Pnative -Dagent exec:exec@java-agent
The application will return a random saying. The agent generates the configuration files in the target/native/agent-output subdirectory.
-
Build a native executable of this application with GraalVM Native Image and Maven:
mvn -Pnative -Dagent package
When the command completes, a native executable,
fortune
, is generated in the target directory of the project and ready for use. -
Run the application by launching a native executable directly or with the Maven profile:
./target/fortune
mvn -Pnative exec:exec@native
-
Change to the fortune-demo/fortune-gradle directory:
cd ../fortune-gradle
-
Build the project:
./gradlew run
-
Run your application with the agent, on the JVM. To enable the agent with the Native Image Gradle plugin, pass the
-Pagent
option to any Gradle tasks that extendsJavaForkOptions
:./gradlew -Pagent run
The agent captures and records the dynamic features encountered during a test run into multiple *-config.json files.
-
Once the metadata is collected, copy it into the project’s /META-INF/native-image/ directory using the
metadataCopy
task:./gradlew metadataCopy --task run --dir src/main/resources/META-INF/native-image
-
Build a native executable using configuration collected by the agent:
./gradlew nativeCompile
When the command completes, a native executable,
fortune
, is generated in the build/native/nativeCompile directory of the project and ready for use. -
Run the application from the native executable:
./fortune/build/native/nativeCompile/fortune
The StaticFortune project contains an enhanced version of the same application and uses the Maven plugin for GraalVM Native Image building.
-
Change to the project directory:
cd ../staticfortune
-
Build the project:
mvn clean package
-
Run your application with the agent, on the JVM:
mvn -Pnative -Dagent exec:exec@java-agent
The application will print a random saying. The agent generates the configuration files in the target/native/agent-output subdirectory.
-
Build a static native executable:
mvn -Pnative -Dagent package
When the command completes, a native executable,
staticfortune
, is generated in the target directory of the project and ready for use. -
Run the demo by launching a native executable directly or with the Maven profile:
./target/staticfortune
mvn -Pnative exec:exec@native
To see the benefits of executing these applications as native executables, time the execution and compare with running on the JVM.