Ktor 3.0.2 Help

GraalVM

Ktor Server applications can make use of GraalVM in order to have native images for different platforms, and of course, take advantage of the faster start-up times and other benefits that GraalVM provides.

Currently, Ktor Server applications that want to leverage GraalVM have to use CIO as the application engine.

Prepare for GraalVM

In addition to installing GraalVM and having the installation directory in the system path, you need to prepare your application so that all dependencies are bundled, i.e. you need to create a fat jar.

Reflection configuration

GraalVM has some requirements when it comes to applications that use reflection, which is the case of Ktor. It requires that you provide it a JSON file with certain type information. This configuration file is then passed as an argument to the native-image tool.

Execute the native-image tool

Once the fat jar is ready, the only step required is to create the native image using the native-image CLI tool. This could be also done by Gradle plugin. You can see the example of the build.gradle.kts file here. However, please note that some options may vary depending on the dependencies being used, the package name of your project, etc.

Run the resulting binary

If the shell script executes without any errors, you should obtain a native application, which in the case of the sample is called graal-server. Executing it will launch the Ktor Server, responding on https://0.0.0.0:8080.

Last modified: 02 April 2024