The JRush web conference for Java engineers is coming soon! Register now and evolve your Java apps. Rush with us!

New NIK 21.3 release with AWT and OpenJFX support

The New Liberica Native Image Kit 21.3 based on Liberica 17 and 11 is out!


Published October 22, 2021


Liberica 17 一 the new base for NIK 21.3

The new LTS release of Liberica JDK is the event that leads to many new releases of our products. We present the latest version of Liberica NIK, a tool based on GraalVM that accelerates your application by utilizing native image technology.

It is based on Liberica JDK 17 or Liberica JDK 11, two of the latest LTS versions.

Contents

  1. Why Liberica NIK?
    1. New version of GraalVM in Liberica NIK
    2. Spring Boot Native depends on Liberica JDK
  2. Enhanced desktop support
    1. Liberica NIK + AWT/Swing example project
    2. Liberica NIK + OpenJFX example project
  3. Open source software approach
  4. Conclusion

1. Why Liberica NIK?

There are many reasons to choose Liberica NIK as a powerhouse for your native images!

New version of GraalVM in Liberica NIK

The Graal Virtual Machine that powers Liberica NIK has also been updated to the latest version that comes with an abundance of new features:

  • Initial support for Java module system in native-image. The following command line options are supported: “--module”, “--module-path”, “--add-exports”, and “--add-opens”. They work the same way as in the Java launcher, so now it’s possible to natively compile an application that consists of several modules;
  • You can now configure Reflection and JNI configuration file entries to apply only in the case when a particular class is reachable during analysis. This feature helps in producing smaller and faster native images;
  • The new “adaptive” garbage collection policy helps to reduce memory footprint and to make GC more performant. It is now used by default. The previous policy “BySpaceAndTime” can be utilized by running the “-H:InitialCollectionPolicy” command line option;
  • The native image generator can now read command line arguments from a file using the “@file” syntax the way javac does.

Spring Boot Native depends on Liberica JDK

Liberica JDK is the default runtime utilized with Spring applications, making Liberica NIK the best utility for producing Spring Boot native images.

2. Enhanced desktop support

In this new update, we provide enhanced support for desktops. We introduce the new “Full” Liberica NIK flavor including OpenJFX.

Liberica NIK now works with AWT/Swing (core, standard and full builds of Liberica NIK for Linux; based on Liberica JDK 11) and OpenJFX (only full builds for Linux and Windows; based on Liberica JDK 11 and 17).

List of Liberica NIK 21.3 supported platforms

Liberica JDK base for Liberica NIK + OS & CPU Headless
(non-GUI) applications support
AWT/Swing OpenJFX (in full builds only)
Liberica JDK 11 + Windows x86 + - +
Liberica JDK 11 + Linux x86 + + +
Liberica JDK 11 + Linux ARM + - -
Liberica JDK 11 + Alpine Linux x86 + - -
Liberica JDK 11 + Alpine Linux ARM + - -
Liberica JDK 11+ macOS x86 + - -
Liberica JDK 17 + Windows x86 + - +
Liberica JDK 17+ Linux x86 + - +
Liberica JDK 17 + Linux ARM + - -
Liberica JDK 17 + Alpine Linux x86 + - -
Liberica JDK 17 + Alpine Linux ARM + - -
Liberica JDK 17+ macOS x86 + - -

We plan to add support for AWT/Swing in Liberica NIK for Windows and Mac and OpenJFX for Mac in the future releases.

Note that the full support of OpenJFX has already been implemented for some time in full builds of Liberica JDK 8, 11, 17.

Liberica NIK + AWT/Swing example project

To display the Liberica NIK at work, we present how to convert a Swing application to a native image. In this example, we will use the Stylepad demo application. Remember, you need to use the Liberica NIK build based on Liberica JDK 11 for this to work!

Before we start

The first thing to remember is that to build a native image for a Swing application, you need to keep some details in mind:

  • Graal sets java.awt.headless property to true by default. Don’t forget to explicitly set java.awt.headless property to false when generating a native image for swing applications;
  • Explicitly provide resources used by the application such as property files and images to the native image tool;
  • Do the same with classes used by reflection or serialization in the application: explicitly provide them to the native image tool.

Getting the project ready

Note that the Stylepad JDK application uses serialization. Run the Stylepad demo with Graal tracing agent to dump the serialization classes used by the demo:

java -agentlib:native-image-agent=config-output-dir=conf-dir -jar Stylepad.jar

Set java.awt.headless property to false. Then provide *.properties, *.gif resource files, and the serialization config file:

native-image -Djava.awt.headless=false '-H:IncludeResources=.*/(Notepad|Stylepad).*properties$' '-H:IncludeResources=.*/.*gif$' -H:SerializationConfigurationFiles=conf-dir/serialization-config.json -jar Stylepad.jar

alt_text

Image taken from natively compiled Stylepad demo

Building the native image

Run the Stylepad demo with Graal tracing agent to dump the resources and reflection classes used by the demo:

java -agentlib:native-image-agent=config-output-dir=conf-dir -jar Stylepad.jar

Run the native image tool with resource and reflection configuration files that you just generated:

native-image -Djava.awt.headless=false -H:ResourceConfigurationFiles=conf-dir/resource-config.json -H:ReflectionConfigurationFiles=conf-dir/reflect-config.json -jar Stylepad.jar

Discussion

Note that to make the NIK AOT compilation work with the project sources, we had to trace and update this project with:

  • Classes used in reflection (Toolkit, FontManager, L&Fs, KeyEvent);
  • Resources used by L&Fs.

Swing classes used by reflection or JNI were added to the current version of Liberica NIK. They were obtained by using the tracing agent and run with several Swing applications.

It is possible that some usages of reflection or JNI are missed. To solve this, you need to run the tracing agent and add the missed classes manually.

Numeric results

Size comparison

jdeps --list-deps Stylepad.jar
java.base
java.desktop
java.logging

jlink --no-header-files --no-man-pages --compress=2 --strip-debug --add-modules java.desktop,java.logging --output jdk-desktop

alt_text

Size comparison of different kinds of Stylepad app packaging

Starting Stylepad demo by jar file:

time -v jdk-desktop/bin/java -jar Stylepad.jar
    User time (seconds): 1.89
    System time (seconds): 0.09
    Percent of CPU this job got: 17%
    Maximum resident set size (kbytes): 72192

Starting Stylepad native image:

time -v ./Stylepad
    User time (seconds): 0.10
    System time (seconds): 0.05
    Percent of CPU this job got: 1%
    Maximum resident set size (kbytes): 71760

Known Limitations

  • The current AWT/Swing support is implemented only on Linux OS and for Liberica NIK builds based on
    Liberica JDK 11;
  • Gradients and borders are not correctly drawn on JButton for Nimbus L&F. Use other design solutions;
  • There is the native image linker failure for applications that explicitly use the SplashScreen class.

Future work

In the subsequent releases, we plan to

  • Add AWT/Swing support for Windows and macOS platforms;
  • Resolve issues for applications utilizing a splash screen.

Liberica NIK + OpenJFX example project

Our second example involves using OpenJFX to build the native image of the “Brick Breaker” game.

This solution works on both builds of Liberica NIK (based on Liberica JDK 11 or 17), and both supported OS: Windows and Linux.

Building the native image

First, download & install nik-openjdk11-full-21.3 from bell-sw.com.

Run the command

native-image -H:IncludeResources='ensemble.samples.shared-resources.brickImages.*' -jar BrickBreaker.jar brickbreaker

To include the resources (such as images, sounds, or XML files) in the native image, execute the following line:

-H:IncludeResources='com.fxapp.package.resources.*'

To find all the available usages see the GraalVM documentation for more options.

Then be patient and wait for the process to finish.

Run ./brickbreaker

alt_text

Image taken from natively compiled BrickBreaker OpenJFX demo

Discussion

Performance results

Size RSS Startup
Liberica 11.0.13-full + BrickBreaker.jar 517M 587M 360ms
Custom image (jlink --add-modules javafx.graphics) + BrickBreaker.jar 122M 422M 360ms
Native image 33M 384M 270ms

Limitations and how to overcome them

  • Currently available for Windows and Linux x64 only;
  • javafx.media and javafx.web modules are not supported at the moment;
  • Reflective access to classes/methods/fields (including JNI) should be configured before running native-image. We handle reflective calls made by OpenJFX libraries for you, but you still should properly configure those made by your application.

Future work

macOS support is planned for 2022.

Got any questions? Book a free consultation!

3. Open source software approach

BellSoft, the creators of Liberica JDK and Liberica NIK, follow the open source approach to development. We continuously push changes upstream to the mainline of OpenJDK. We participate on the GraalVM advisory board. This makes us experts on native image technology. Our software is preferred by companies that value the stability and security of runtime. The latest example is VMware who chose Liberica NIK as a fully supported end-to-end native solution for Spring Boot native images.

4. Conclusion

The new Liberica NIK v21.3 is the reliable and tested solution for building native images with Spring, and now it works perfectly with desktop native images, bringing the stability, security, and support of Liberica JDK to Linux and Windows desktop apps. Bellsoft will continue further development of NIK with more features and supported systems to come in the future.

Author image

Peter Zhelezniakov

Software Engineer at BellSoft