GraalVM demos: AOT compilation of an application using Java and Kotlin

This is an example of a GraalVM demo application that shows ahead-of-timecompilation of Java and Kotlin code.

Prerequisites

Preparation

  • Download or clone the repository and navigate into the java-kotlin-aot directory:
  1. git clone https://github.com/graalvm/graalvm-demos
  2. cd graalvm-demos/java-kotlin-aot

This is a simple Java / Kotlin application showing how easy it is to interopbetween JVM-based languages. A Java method accesses a String from Kotlin andcalls a Kotlin function, which later accesses a String from a Java class. Beforerunning this example, you need to build the application. Note that you can useany JDK for building the application, however we refer to javac from GraalVMin the build script to simplify the prerequisites and not to depend on anotherJDK installed.

  • Having downloaded and unzipped GraalVM CE or EE archive, export the GraalVM home directory as the $GRAALVM_HOME and add $GRAALVM_HOME/binto the path, using a command-line shell for Linux:
  1. export GRAALVM_HOME=/home/${current_user}/path/to/graalvm

and for macOS:

  1. export GRAALVM_HOME=/Users/${current_user}/path/to/graalvm/Contents/Home

Note that your paths are likely to be different depending on the download location.

  • Make sure the native-image utility is available. Starting from GraalVM 19.0, Native Image was extracted from the base distribution. This functionality can be added to the core installation with GraalVM Updater tool by running:gu install native-image.

  • Then execute:

  1. ./build.sh

Have a look at the build.sh script which creates a native image from the Java class.The native-image utility compile the application ahead-of-time for faster startup and lower general overhead at runtime.

  1. $GRAALVM_HOME/bin/native-image -cp ./target/mixed-code-hello-world-1.0-SNAPSHOT.jar -H:Name=helloworld -H:Class=hello.JavaHello -H:+ReportUnsupportedElementsAtRuntime --allow-incomplete-classpath

It takes a couple of parameters, the classpath, the main class of the application with-H:Class=… and the name of the resulting executable with -H:Name=….

After executing the native-image command, check the directory, it should haveproduced an executable file helloworld.

Running the application

To run the application, you need to execute the fat jar file in the target dir.You can run it as a normal Java application using java.Or, since we have a native image prepared, you can run that directly.The run.sh file executes both, and times them with the time utility.

  1. java -cp ./target/mixed-code-hello-world-1.0-SNAPSHOT-jar-with-dependencies.jar hello.JavaHello
  2. ./helloworld

Approximately, the following output should be produced:

  1. ./run.sh
  2. + java -cp ./target/mixed-code-hello-world-1.0-SNAPSHOT-jar-with-dependencies.jar hello.JavaHello
  3. Hello from Kotlin!
  4. Hello from Java!
  5. real 0m0.129s
  6. user 0m0.094s
  7. sys 0m0.034s
  8. + ./helloworld
  9. Hello from Kotlin!
  10. Hello from Java!
  11. real 0m0.010s
  12. user 0m0.003s
  13. sys 0m0.004s

The performance gain of the native version is largely due to the faster startup.

License

The sample application in this directory is taken from the JetBrains Kotlin-examples repository.It is distributed under the Apache License 2.0.