How to Use Native Image Kit With the Quarkus Framework
In this tutorial, learn how to use Liberica Native Image Kit, which is designed to support numerous platforms and programming languages, with Quarkus.
Join the DZone community and get the full member experience.Join For Free
BellSoft issued a new version of Liberica Native Image Kit (NIK), 220.127.116.11, so in this short article, I will provide a tutorial on how to use it with the Quarkus framework.
Liberica NIK is based on the open-source project GraalVM (Community Edition) and is compatible with many platforms, including lightweight musl-based Alpine Linux. Liberica NIK in essence is a utility that converts JVM-based applications into native executables (AOT compilation).
The latest release of Liberica NIK 18.104.22.168 includes several enhancements:
- Added support for OpenJFX in Liberica NIK for macOS (Windows and Linux are already supported)
- Native-image default inclusion in all NIK flavors. Running GU is no longer required for installation.
Why Would You Use Native Image?
Native image technology optimizes resource consumption, minimizes the static footprint, and delivers almost instant startup. Liberica NIK is designed to support a large number of platforms and programming languages, and therefore this kit provides an opportunity to create multilingual programs. The end result of utilizing Liberica NIK is cost reduction and accelerated deployment.
How Can You Use Liberica NIK With the Quarkus Framework?
Quarkus is an open-source stack of technologies for the cloud-native world. While traditional Java stacks were made for monolithic applications, Quarkus is a new wave framework aiming to decrease large memory requirements in the industry where the cloud and containers play the leading role now. It is a Kubernetes-native Java framework and is tailored to the native images approach, perfect for deployment in the cloud and serverless development. Quarkus supports several popular standards of Jakarta EE/MicroProfile implementation.
By using Liberica NIK with the Quarkus framework, you can actually boost its efficiency even more.
Let's Build a Native Image
So let's build a native image out of the Hello World application as an example, using Quarkus and Liberica NIK. To begin, you need to prepare all the necessary tools and software.
First, set up a working C compiler toolchain. On Linux, GCC and the glibc and zlib headers are necessary.
# dnf (rpm-based)
sudo dnf install gcc glibc-devel zlib-devel libstdc++-static
# Debian-based distributions:
sudo apt install build-essential libz-dev zlib1g-dev
On Windows, you will have to install the Visual Studio 2017 Visual C++ Build Tools.
In the case of MacOS, the dependencies are provided by XCode:
To demonstrate how to integrate Liberica NIK into Quarkus, we will use the basic Quarkus “Hello World” application. Go ahead and follow the instructions on the site to create a simple Quarkus app.
Now that you have your Hello World application ready, let’s configure the Native Image Kit.
Start with downloading the appropriate version of Liberica NIK (the package already contains Liberica VM and native-image tool). After the download is finished, check the file by verifying the checksum in the command line (the checksum should match the one next to the link on the downloads page).
Configure build environment. For Linux and macOS, in case you have TGZ/ZIP archive, set
JAVA_HOME environment variable to the NIK installation directory:
If the installation is performed using the package (deb, pkg/dmg), the installation path for macOS will be the conventional one.
On Windows, you will have to go through the Control Panel to set your environment variables.
Please note, that all releases of Liberica NIK starting from 21.3.1 include a native image by default, allowing you to skip the step of gu install. So I will not be describing this step here as it is not required in our process.
If you followed the instructions and constructed the Quarkus application, you will find the following profile in the pom.xml:
The native executable for your application will contain the application code, necessary libraries, Java APIs, as well as a special reduced version of a virtual machine. To create it, run the following:
JAVA_HOME=$HOME/Development/bellsoft-liberica-vm-openjdk11-22.214.171.124./mvnw package -Pnative
Note that it may take some time (several minutes usually) to package the native executable, so be patient. You also don’t need to prepend every command with setting
JAVA_HOME if you export it as described in step 1.
Here we are! We have made a native executable for your application! Get the resulting executable file as
To ensure everything works flawlessly, launch the application.
If you have followed through this short exercise you are able to reach an incredible application start time of only 0.013 seconds with the latest Liberica Native Image Kit 126.96.36.199. Enjoy your development experience with this utility!
Opinions expressed by DZone contributors are their own.