Building a JavaFX Native Image
Introduction
Section titled “Introduction”JavaFX is an open-source platform for building rich graphical client applications in Java. It is the modern successor to Swing and offers CSS styling, FXML declarative layouts, property binding, animations, and built-in 2D/3D graphics.
This guide walks you through building a small JavaFX application and then compiling it into a self-contained native image with Liberica NIK. A native image starts in milliseconds, needs no JVM on the target machine, and ships as a single executable — a good fit for desktop distribution.
Liberica NIK Full bundles LibericaFX (an instance of OpenJFX), so you do not need a separate JavaFX SDK to compile JavaFX applications natively.
Prerequisites
Section titled “Prerequisites”- Liberica NIK Full for your platform. Download it from the Liberica NIK Download Center and follow the installation instructions. Pick the Full package — it includes LibericaFX.
- Maven to build the application JAR.
After installing, put NIK on your PATH and verify it:
export PATH=<path-to-nik>/bin:$PATHnative-image --versionBuild the JavaFX application
Section titled “Build the JavaFX application”We will build a minimal FXML-based app: a text field, a button, and a label that greets the name you type.
Application class
Section titled “Application class”The entry point extends javafx.application.Application and loads the UI from an
FXML file:
package dev.cat;
import javafx.application.Application;import javafx.fxml.FXMLLoader;import javafx.scene.Scene;import javafx.scene.layout.BorderPane;import javafx.stage.Stage;
public class Main extends Application {
@Override public void start(Stage primaryStage) throws Exception { FXMLLoader loader = new FXMLLoader(Main.class.getResource("/home.fxml")); BorderPane pane = loader.load();
Scene scene = new Scene(pane); primaryStage.setTitle("Hello JavaFX"); primaryStage.setScene(scene); primaryStage.show(); }
public static void main(String[] args) { launch(args); }}FXML layout
Section titled “FXML layout”Place home.fxml in src/main/resources. The fx:controller attribute binds
the layout to a controller class, and fx:id/onAction wire nodes to fields and
handlers:
<?xml version="1.0" encoding="UTF-8"?><?import javafx.scene.control.*?><?import javafx.scene.layout.*?>
<BorderPane xmlns:fx="http://javafx.com/fxml" fx:controller="dev.cat.HelloController"> <center> <VBox spacing="10"> <TextArea fx:id="text"/> <Button text="Say hello" onAction="#sayHello"/> <Label fx:id="dataLabel"/> </VBox> </center></BorderPane>Controller
Section titled “Controller”The controller uses @FXML to inject nodes and bind a property to the label:
package dev.cat;
import javafx.beans.property.SimpleStringProperty;import javafx.beans.property.StringProperty;import javafx.fxml.FXML;import javafx.fxml.Initializable;import javafx.scene.control.Label;import javafx.scene.control.TextArea;
import java.net.URL;import java.util.ResourceBundle;
public class HelloController implements Initializable { @FXML private TextArea text; @FXML private Label dataLabel;
private final StringProperty name = new SimpleStringProperty();
@FXML void sayHello() { name.setValue("Hello, " + text.getText() + "!"); }
@Override public void initialize(URL location, ResourceBundle resources) { dataLabel.textProperty().bind(name); }}Package an executable JAR
Section titled “Package an executable JAR”Add the JavaFX dependencies and a manifest pointing at Main to your pom.xml:
<dependencies> <dependency> <groupId>org.openjfx</groupId> <artifactId>javafx-controls</artifactId> <version>21</version> </dependency> <dependency> <groupId>org.openjfx</groupId> <artifactId>javafx-fxml</artifactId> <version>21</version> </dependency></dependencies>
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <configuration> <archive> <manifest> <mainClass>dev.cat.Main</mainClass> </manifest> </archive> </configuration> </plugin> </plugins></build>Build the JAR:
mvn clean packageCompile a native image with NIK
Section titled “Compile a native image with NIK”FXML drives the UI through reflection: FXMLLoader instantiates the controller
and injects @FXML fields by name, and the controller class is named only in the
FXML file — not in code native-image can see. So you must tell native-image
about those reflective accesses and bundle the FXML and any images as resources.
Generate reachability metadata
Section titled “Generate reachability metadata”The simplest, least error-prone way to capture reflection, resource, and JNI usage is to run the app on the JVM under the native-image tracing agent. It records what the app actually touches and writes ready-to-use configuration files.
-
Run your application with the agent, exercising every screen so all code paths are observed:
Terminal window java -agentlib:native-image-agent=config-output-dir=src/main/resources/META-INF/native-image \-jar target/hello-javafx-1.0.jar -
Use the app (type a name, click the button, open every window), then close it. The agent writes
reflect-config.json,resource-config.json, and related files into the output directory.native-imagepicks up configuration fromMETA-INF/native-imageautomatically.
Build the executable
Section titled “Build the executable”Resources such as the FXML files and icons must be explicitly included.
You can pass them on the command line with -H:IncludeResources (a regular
expression), in addition to anything the agent already captured:
native-image -H:IncludeResources='.*\.fxml$' \ -jar target/hello-javafx-1.0.jar hello-javafxRun the resulting executable:
./hello-javafxResult
Section titled “Result”The native image launches the same JavaFX window, but starts faster and uses less memory than the JVM-plus-JAR equivalent, and runs without a separate Java installation. For a heavier example and measured startup/memory figures, see Using NIK with desktop applications.