Skip to content

Latest commit

 

History

History

fortune-demo

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

Native Image Fortune Demo

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:

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.

Preparation

  1. Download and install the latest GraalVM for JDK 21 using SDKMAN!.

    sdk install java 21.0.3-graal
  2. Download or clone GraalVM demos repository:

    git clone https://github.com/graalvm/graalvm-demos

Fortune Maven Demo

  1. Change to the fortune-demo/fortune-maven directory:

    cd fortune-demo/fortune-maven
  2. Build the project:

    mvn clean package
  3. 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.

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

  5. Run the application by launching a native executable directly or with the Maven profile:

    ./target/fortune
    mvn -Pnative exec:exec@native

Fortune Gradle Demo

  1. Change to the fortune-demo/fortune-gradle directory:

    cd ../fortune-gradle
  2. Build the project:

    ./gradlew run
  3. 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 extends JavaForkOptions:

    ./gradlew -Pagent run

    The agent captures and records the dynamic features encountered during a test run into multiple *-config.json files.

  4. 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
  5. 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.

  6. Run the application from the native executable:

    ./fortune/build/native/nativeCompile/fortune

StaticFortune

The StaticFortune project contains an enhanced version of the same application and uses the Maven plugin for GraalVM Native Image building.

  1. Change to the project directory:

    cd ../staticfortune
  2. Build the project:

    mvn clean package
  3. 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.

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

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

Learn More