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 FreeBellSoft issued a new version of Liberica Native Image Kit (NIK), 22.0.0.2, 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 22.0.0.2 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: xcode-select --install
.
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:
export JAVA_HOME=$HOME/Development/bellsoft-liberica-vm-openjdk11-22.0.0.2
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:
<profiles>
<profile>
<id>native</id>
<properties>
<quarkus.package.type>native</quarkus.package.type>
</properties>
</profile>
</profiles>
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.0.0.2./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 target/getting-started-1.0.0-SNAPSHOT-runner
.
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 22.0.0.2. Enjoy your development experience with this utility!
Opinions expressed by DZone contributors are their own.
Comments