BellSoft has released a new version of The Liberica Native Image Kit (NIK), 22.0.0.2. In this article, we talk about how to use the Native Image Kit with the Quarkus framework.

Liberica NIK is based on the open source project GraalVM (Community edition) and is compatible with multiple platforms, including Alpine Linux, which is based on lightweight MusL. Liberica NIK is essentially a utility that converts JVA-BASED applications into native executables (AOt-compiled).

The latest version of Liberica NIK 22.0.0.2 includes a number of enhancements:

  • Added support for OpenJFX in Liberica NIK for macOS (already supports Windows and Linux)
  • Native images are included in all NIK styles by default. Installation no longer requires running GU.

Why use native art?

Native Image technology optimizes resource consumption, minimizes static footprint, and provides almost real-time startup. Liberica NIK is designed to support a large number of platforms and programming languages, so the suite supports programs in multiple languages. Using Liberica NIK reduces costs and speeds deployment.

How do I use Liberica NIK in the Quarkus framework?

Quarkus is a cloud-native open source technology stack. While the traditional Java stack was designed for monolithic applications, Quarkus is a new wave framework designed to reduce large memory requirements in the cloud and container-dominated industry. It is a Kubernetes native Java framework tailored for native images, ideal for cloud deployment and serverless development. Quarkus supports several popular standards implemented by Jakarta EE/MicroProfile.

By using Liberica NIK in conjunction with the Quarkus framework, its efficiency can be further improved.

Build a native image

So let’s take the Hello World application as an example and use Quarkus and Liberica NIK to build native images. First, you need to prepare tools and software.

Set up a C compiler toolchain. On Linux, GCC is required along with glibc and zlib headers.

# 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
Copy the code

Install Visual Studio 2017 Visual C++ Build Tools on Windows.

In the case of MacOS, dependencies are provided by XCode: xcode-select –install.

To demonstrate how Liberica NIK can be integrated into Quarkus, we will use the basic Quarkus “Hello World” application. Continue with the official documentation to create a simple Quarkus application.

Now that you have the Hello World application ready, let’s configure the Native Image Kit.

Start by downloading the appropriate version of Liberica NIK (this package already includes the Liberica VM and native imaging tools). Once the download is complete, check the file by validating the checksum on the command line (the checksum should match the checksum next to the link on the download page).

Configure the build environment. For Linux and macOS, if you have a TGZ/ZIP archive, set the JAVA_HOME environment variable to the NIK installation directory:

Export JAVA_HOME = $HOME/Development/bellsoft liberica -- - openjdk11-22.0.0.2 vm

If you install using a package (deb, PKG/DMG), the macOS installation path is the normal path.

On Windows, use the control panel to set environment variables.

Note that all Liberica NIK releases starting with 21.3.1 include native images by default, allowing you to skip the GU install step. We do not describe this step here.

If you follow the instructions to build the Quarkus application, you will find the following configuration files in pom.xml:

<profiles>
    <profile>
        <id>native</id>
        <properties>
            <quarkus.package.type>native</quarkus.package.type>
        </properties>
    </profile>
</profiles>
Copy the code

The native executable of the application will contain the application code, the necessary libraries, the Java API, and a special simplified version of the virtual machine. To create it, run the following command:

JAVA_HOME = $HOME/Development/bellsoft - liberica - vm - openjdk11-22.0.0.2. / MVNW package - Pnative

Note that it may take some time (usually a few minutes) to package the native execution file, so please be patient. JAVA_HOME If you export commands as described in Step 1, you do not need to prefix each command with Settings.

The generated executable is used as target/getting-started- 1.0.0-snapshot-runner.

To make sure everything works, start the application.

If you complete this short exercise, you will be able to achieve an incredible application startup time (just 0.013 seconds) using the latest Liberica Native Image Kit 22.0.0.2. Enjoy your development experience with this utility!