A Quick Intro to GraalVM

May 07, 19

Introduction

Oracle recently announced the production ready 1.0 release of GraalVM. If you have not been following the developments around the polyglot VM offering from Oracle here is a good resource from Oracle Oracle Blog

Installation

Download the latest version from Download GraalVM. For this tutorial, I shall be using the open source version from here Next, we will install the native-image tool to create a native binary.

gu install native-image

HelloWorld Example

fun main() {
    println("Hello, World!")
}

Compile the Kotlin code

kotlinc Hello.kt

Run the code

kotlin HelloKt

Now, to generate the native executable, we first need to generate a jar

kotlinc Hello.kt -include-runtime -d hello.jar

Lastly, we need to run this command to generate the native executable

native-image -jar hello.jar

I got following error at this step.

Build on Server(pid: 16113, port: 42559) [hello:16113] classlist: 407.09 ms [hello:16113] (cap): 227.08 ms [hello:16113] setup: 323.12 ms Error: Basic header file missing (<zlib.h>). Make sure headers are available on your system. Error: Use -H:+ReportExceptionStackTraces to print stacktrace of underlying exception Error: Image build request failed with exit status 1

On further research https://www.graalvm.org/docs/reference-manual/aot-compilation/ I had to install glibc-devel, zlib-devel and gcc library

The instructions in this section may vary based on your OS version. I ran these commands on a Ubuntu 19.04 desktop.

sudo apt-get update

sudo apt-get install build-essential

sudo apt install gcc

sudo apt install zlib1g-dev

Re-run the previous native-image command and this should generate a native executable in your current folder.