Skip to content

Building a JavaFX Native Image

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.

After installing, put NIK on your PATH and verify it:

Terminal window
export PATH=<path-to-nik>/bin:$PATH
native-image --version

We will build a minimal FXML-based app: a text field, a button, and a label that greets the name you type.

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);
}
}

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>

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);
}
}

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:

Terminal window
mvn clean package

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.

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.

  1. 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
  2. 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-image picks up configuration from META-INF/native-image automatically.

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:

Terminal window
native-image -H:IncludeResources='.*\.fxml$' \
-jar target/hello-javafx-1.0.jar hello-javafx

Run the resulting executable:

Terminal window
./hello-javafx

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.