Encodera

Thanks for reading!

Donate to the site — we work hard for you.

Contents

Creating a project

A JavaFX project is an ordinary Maven project with two particulars: the library comes in as the javafx-controls dependency, and the application is started through the javafx-maven-plugin plugin. The plugin assembles the right options for Java, so the window opens with a single command.

Creating from the archetype

An archetype is a Maven project template. OpenJFX has an official archetype, javafx-archetype-simple:

mvn archetype:generate -DarchetypeGroupId=org.openjfx -DarchetypeArtifactId=javafx-archetype-simple -DarchetypeVersion=0.0.6 -DgroupId=com.example -DartifactId=notes -Dpackage=com.example.notes -Djavafx-version=25.0.4 -DinteractiveMode=false
cd notes

Maven creates a notes folder with a ready structure:

FileWhat is inside
pom.xmlDependencies and plugins
src/main/java/module-info.javaThe module description
src/main/java/com/example/notes/App.javaThe main application class
src/main/java/com/example/notes/SystemInfo.javaA helper class of the sample, you can delete it

Later a src/main/resources/com/example/notes/ folder will appear for FXML and CSS files.

pom.xml

The archetype writes outdated settings: Java 11 and old plugin versions. Replace pom.xml completely:

<?xml version="1.0" encoding="UTF-8"?>
<!-- pom.xml -->
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
 
    <groupId>com.example</groupId>
    <artifactId>notes</artifactId>
    <version>1.0-SNAPSHOT</version>
 
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.release>25</maven.compiler.release>
        <javafx.version>25.0.4</javafx.version>
    </properties>
 
    <dependencies>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-controls</artifactId>
            <version>${javafx.version}</version>
        </dependency>
    </dependencies>
 
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.14.1</version>
            </plugin>
            <plugin>
                <groupId>org.openjfx</groupId>
                <artifactId>javafx-maven-plugin</artifactId>
                <version>0.0.8</version>
                <configuration>
                    <mainClass>com.example.notes/com.example.notes.App</mainClass>
                    <options>
                        <option>--enable-native-access=javafx.graphics</option>
                    </options>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

javafx-controls pulls in javafx-graphics and javafx-base; Maven picks the build for your operating system itself. mainClass holds the module name and the class name separated by /. The --enable-native-access option removes the Java 25 warning about JavaFX loading its native libraries.

module-info.java

A JavaFX project is a Java module. This file says which modules it needs and what it exposes:

// src/main/java/module-info.java
module com.example.notes {
    requires javafx.controls;
    exports com.example.notes;
}

requires brings in the JavaFX module, and exports lets JavaFX create an object of our App class.

The main class

Replace App.java with a short version and delete SystemInfo.java:

// src/main/java/com/example/notes/App.java
package com.example.notes;
 
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
 
public class App extends Application {
 
    @Override
    public void start(Stage stage) {
        Label label = new Label("Hello, JavaFX " + System.getProperty("javafx.version"));
        Scene scene = new Scene(new StackPane(label), 640, 480);
        stage.setTitle("Notes");
        stage.setScene(scene);
        stage.show();
    }
 
    public static void main(String[] args) {
        launch(args);
    }
}

The class extends Application, and the start method builds the window. The next lesson covers it in detail.

Running

mvn javafx:run

Maven compiles the project and opens a 640 × 480 window titled "Notes". In the middle is the label "Hello, JavaFX 25.0.4". The terminal stays busy while the window is open; after you close it, Maven prints BUILD SUCCESS.

Common mistake

If you run App from the IDE as an ordinary class with a main method, the program stops at once with Error: JavaFX runtime components are missing, and are required to run this application. With such a launch the JavaFX libraries do not get into the module list. Run it with mvn javafx:run; in IntelliJ IDEA this command is on the Maven tool window under Plugins → javafx → javafx:run.

Thanks for reading!

Donate to the site — we work hard for you.