This example demonstrates the ability of native-image
to run parts of your application at the build time.
In both examples we use the Jackson framework to parse a JSON file to determine which Handler
should be used by the application (CurrentTimeHandler
or HelloWorldHandler
) at runtime.
-
In
configure-at-runtime-example
the JSON parsing happens at image run time and thus contributes to the image startup time. In addition, all methods and static fields originated from the Jackson framework that are needed become part of a native executable. -
In contrast,
configure-at-buildtime-example
performs the JSON parsing as part of building the native executable. In this case the executable does not contain any parts of the Jackson framework (can be verified easily by adding-H:+PrintAnalysisCallTree
to the<buildArgs>
inpom.xml
). When this image gets executed, it can run right away since it was already determined at build time which handler should be used.
-
Download and install the latest GraalVM JDK using SDKMAN!.
sdk install java 21.0.1-graal
-
Download or clone the repository and navigate into the
native-image-configure-examples
directory:git clone https://github.com/graalvm/graalvm-demos
cd graalvm-demos/native-image-configure-examples
- Change to one of the demo subdirectories, for example,
configure-at-runtime-example
:cd graalvm-demos/native-image-configure-examples/configure-at-runtime-example
- Build the project:
mvn package
- Once the Maven build succeeds, a native executable called "runtime-example" will be generated in the
configure-at-runtime-example/target/
directory. Run it:You will see the current date and time message../target/runtime-example
- Repeat the same steps for the other sub-demo:
cd ../configure-at-buildtime-example
mvn package
You will see the next output:./target/buildtime-example
Hello, world!
Loading application configuration at executable build time can speed up application startup.