Download and Setup JavaFX

JavaFX is an open source, next generation client application platform for desktop, mobile and embedded systems built on Java. It provides an API for designing GUI applications that run on every device supported by Java.

This tutorial will guide you through the installation process of JavaFX on your operating system.

 

Download JavaFX

JavaFX is a standalone component and builds on top of a Java Development Kit. So you should consider installing a Java Development Kit (JDK) that works with the JavaFX version you plan to use, eg. JavaFX 21 requires prior installation of JDK 21. You can find out how to install a JDK by following the setting up a Java Development Kit section of the Getting Started with Java tutorial.

There is a one-stop page that always refers to the latest version of JavaFX and the JDK: https://jdk.java.net/. Selecting the latest "Ready for use" JavaFX version takes you to a page where you can download the version of the JDK you need. From this page you can choose the way to work with JavaFX:

  • Use JavaFX as an SDK to compile and run JavaFX applications.
  • Download the archive containing a series of jmod files and use those with jlink to create a JDK that includes the JavaFX modules, and optionally, your modular application.

You should download JavaFX based on your operating system:

  • Linux/x64
  • macOS/x64
  • macOS/AArch64
  • Windows/x64

This page provides production-ready open-source builds of JavaFX, under the GNU General Public License, version 2, with the Classpath Exception.

 

Setting up a JavaFX SDK for Windows/x64

If you choose to use the JavaFX SDK for Windows/x64, download and unzip it to a desired location. In this section you will use JavaFX 21.0.1.

Add an environment variable pointing to the lib directory of the runtime:

set PATH_TO_FX="path\to\javafx-sdk-21.0.1\lib"

Now you can compile and run JavaFX applications from the command line using the JavaFX runtime. Let's test this by writing a simple HelloWorldFX.java class:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class HelloWorldFX extends Application {

    @Override
    public void start(Stage stage) {
        String javaVersion = System.getProperty("java.version");
        String javafxVersion = System.getProperty("javafx.version");
        Label l = new Label("Hello World running on JavaFX " + javafxVersion + " on top of Java " + javaVersion + ".");
        Scene scene = new Scene(new StackPane(l), 640, 480);
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) {
        launch();
    }

}

First you will compile it with:

javac --module-path %PATH_TO_FX% --add-modules javafx.controls HelloWorldFX.java

When compiling the application you need to add the required modules (javafx.controls). The required modules (javafx.controls) resolve the dependency of transitively required ones (javafx.graphics). In case your application is declaring components via FXML, you should add the javafx.fxml module as well:

javac --module-path %PATH_TO_FX% --add-modules javafx.controls,javafx.fxml HelloWorldFX.java

Now run the application with the following command:

java --module-path %PATH_TO_FX% --add-modules javafx.controls HelloWorldFX

 

Setting up a JavaFX SDK for Linux/x64

In case you choose to use the JavaFX SDK for Linux/x64, download and extract/unpack it to a desired location. In this section you will use JavaFX 21.0.1.

Add an environment variable pointing to the lib directory of the runtime:

export PATH_TO_FX=path/to/javafx-sdk-21.0.1/lib

Now you can compile and run JavaFX applications from the command line using the JavaFX runtime. Let's test this by writing a simple HelloWorldFX.java class:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class HelloWorldFX extends Application {

    @Override
    public void start(Stage stage) {
        String javaVersion = System.getProperty("java.version");
        String javafxVersion = System.getProperty("javafx.version");
        Label l = new Label("Hello World running on JavaFX " + javafxVersion + " on top of Java " + javaVersion + ".");
        Scene scene = new Scene(new StackPane(l), 640, 480);
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) {
        launch();
    }

}

First you will compile it with:

javac --module-path $PATH_TO_FX --add-modules javafx.controls HelloWorldFX.java

When compiling the application you need to add the required modules (javafx.controls). The required modules (javafx.controls) resolve the dependency of transitively required ones (javafx.graphics). In case your application is declaring components via FXML, you should add the javafx.fxml module as well:

javac --module-path $PATH_TO_FX --add-modules javafx.controls,javafx.fxml HelloWorldFX.java

Now run the application with the following command:

java --module-path $PATH_TO_FX --add-modules javafx.controls HelloWorldFX

 

Setting up a JavaFX SDK for macOS

In case you choose to use the JavaFX SDK for macOS, download and extract/unpack it to a desired location. In this section you will use JavaFX 21.0.1.

Add an environment variable pointing to the lib directory of the runtime:

export PATH_TO_FX=path/to/javafx-sdk-21.0.1/lib

Now you can compile and run JavaFX applications from the command line using the JavaFX runtime. Let's test this by writing a simple HelloWorldFX.java class:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class HelloWorldFX extends Application {

    @Override
    public void start(Stage stage) {
        String javaVersion = System.getProperty("java.version");
        String javafxVersion = System.getProperty("javafx.version");
        Label l = new Label("Hello World running on JavaFX " + javafxVersion + " on top of Java " + javaVersion + ".");
        Scene scene = new Scene(new StackPane(l), 640, 480);
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) {
        launch();
    }

}

First you will compile it with:

javac --module-path $PATH_TO_FX --add-modules javafx.controls HelloWorldFX.java

When compiling the application you need to add the required modules (javafx.controls). The required modules (javafx.controls) resolve the dependency of transitively required ones (javafx.graphics). In case your application is declaring components via FXML, you should add the javafx.fxml module as well:

javac --module-path $PATH_TO_FX --add-modules javafx.controls,javafx.fxml HelloWorldFX.java

Now run the application with the following command:

java --module-path $PATH_TO_FX --add-modules javafx.controls HelloWorldFX

 

Working with JavaFX Runtime Images

You can also run your JavaFX application using the set of jmod files available in the JavaFX release page. Start by downloading the archive appropriate for your operating system:

  • Linux/x64
  • macOS/x64
  • macOS/AArch64
  • Windows/x64

In case your operating system is Linux/x64 or macOS, unpack the downloaded archive and add the environment variable pointing to the resulting jmods directory:

export PATH_TO_FX_JMODS=path/to/javafx-jmods-21.0.1

If you are a Windows operating system user, unzip the downloaded archive and add the environment variable pointing to the resulting jmods directory:

set PATH_TO_FX_JMODS="path\to\javafx-jmods-21.0.1"

To use the set of jmod files we need to make HelloWorldFX.java application part of a module. Firstly, you need to add a package declaration to the class:

package helloworld;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class HelloWorldFX extends Application {

  @Override
  public void start(Stage stage) {
    String javaVersion = System.getProperty("java.version");
    String javafxVersion = System.getProperty("javafx.version");
    Label l = new Label("Hello World running on JavaFX " + javafxVersion + " on top of Java " + javaVersion + ".");
    Scene scene = new Scene(new StackPane(l), 640, 480);
    stage.setScene(scene);
    stage.show();
  }

  public static void main(String[] args) {
    launch();
  }

}

Secondly, you have to create a module-info.java where you declare the required modules to run the application:

module helloworldfx {
    requires javafx.controls;
    exports helloworld;
}

Next, you can compile the HelloWorldFX.java application with the JavaFX jmod files:

  • for Linux/x64, macOS/x64, macOS/AArch64
     javac --module-path $PATH_TO_FX_MODS -d mods/helloworldfx $(find . -name "*.java")
    
  • for Windows
    dir /s /b *.java > sources.txt & javac --module-path %PATH_TO_FX_JMODS% -d mods/helloworldfx @sources.txt & del sources.txt
    

Because the helloworldfx is a modular project, you can use jlink to create a custom runtime image using the JavaFX jmods:

  • on Linux/x64, macOS/x64, macOS/AArch64
     $JAVA_HOME/bin/jlink --module-path $PATH_TO_FX_MODS:mods --add-modules helloworldfx --output helloworldfx
    
  • on Windows
    jlink --module-path "%PATH_TO_FX_JMODS%;mods" --add-modules helloworldfx --output helloworldfx
    

After the image is built, you can run it using the following command:

  • on Linux/x64, macOS/x64, macOS/AArch64
    helloworldfx/bin/java -m helloworldfx/helloworld.HelloWorldFX
    
  • on Windows
    helloworldfx\bin\java -m hellofx/helloworld.HelloWorldFX
    

 

Final words

Congratulations on successfully downloading, installing, and using JavaFX!

For further reading, please check out JavaFX Fundamentals.


Last update: November 15, 2023