Posts

Quarkus and Liberica Native Image Kit: it takes two to make a difference

Sep 22, 2021
Dmitry Chuyko
6.2

Several major Java frameworks, such as Spring Boot, support GraalVM Native Image out-of-the box. We discussed how to tranform Spring Boot applications into native images here.

Now, let’s take a look at what Quarkus is capable of in the native image environment.

Quarkus + Liberica Native Image Kit: turn it up!

Quarkus is tailored to native images approach, which is perfect for deployment in the cloud and serverless development. You could be pleasantly surprised to find out that Quarkus efficiency can be boosted even more. How? Use it with a tool, created especially for accelerating your applications ー Liberica Native Image Kit.

Liberica Native Image Kit (NIK) is a GraalVM-based technology that converts a JVM-based application into a fully AOT compiled native executable under the closed-world assumption with an almost instant startup time. It supports a wide variety of platforms, including lightweight musl-based Alpine Linux. Full-fledged support for Alpine Linux musl makes Liberica NIK a perfect choice if you strive to minimize resource consumption, achieve record startup times (up to 1/10 of a second), and save on memory. Moreover, NIK allows you to create seamless polyglot projects in different programming languages.

In addition, NIK runs with most JDK versions, which gives you full control over your build. And the fact that it is based on LTS Liberica JDK means that you can enjoy at least 8 years of access to bug fixes, security updates, and other fixes as needed.

Obviously, you can’t go wrong with the combination of these two powerful tools, Quarkus framework and Liberica NIK. Without further ado, let’s see how to run them together.

How to use Quarkus and NIK together?

Before building a native executable for Quarkus, we need to get all necessary tools and software ready.

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-get 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 are going to use the basic Hello World Quarkus 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 Native Image Kit.

Start with downloading the appropriate version of Liberica NIK (the package already contains Liberica VM, native image and language installables). 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).

  1. Configure the runtime environment. For Linux and macOs, in case you have .tar.gz / zip archive, set JAVA_HOME environment variable to the NIK installation directory:
export JAVA_HOME=$HOME/Development/bellsoft-liberica-vm-openjdk11-21.2.0/

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.

  1. Install the native-image tool using gu install. Alternatively, you can download NIK Core version, which contains Liberica VM and native image without additional languages. In this case, use ni install:
$JAVA_HOME/bin/gu install native-image

As a result, the native executable for your application will contain the application code, necessary libraries, Java APIs, as well as a reduced version of a virtual machine.

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>

  1. To create a native executable, run ./mvnw package -Pnative. Note that it may take some time (several minutes usually) to package the native executable, so be patient.

Here we are! You have built a native executable for your application! Find the resulting executable file as target/getting-started-1.0.0-SNAPSHOT-runner.

To make sure that everything works correctly, launch the application. You should see the following:

The application will listen on http://localhost:8080.

Conclusion

In this article, we discovered the benefits of using Quarkus framework to build cloud-native Java applications and learned how to spike its performance with Liberica Native Image Kit. The process of integrating these technologies is not that complicated, but in the end you get a triple bonus:

  • Small image size to build tiny containers;
  • Outstandingly faster startup time;
  • Significantly reduced memory usage.

Introduce your application to this superhero combo and you won’t be disappointed!

Subcribe to our newsletter

figure

Read the industry news, receive solutions to your problems, and find the ways to save money.

Further reading