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:
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:
First you will compile it with:
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:
Now run the application with the following command:
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:
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:
First you will compile it with:
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:
Now run the application with the following command:
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:
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:
First you will compile it with:
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:
Now run the application with the following command:
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:
If you are a Windows operating system user, unzip the downloaded archive and add the environment variable pointing to the resulting jmods directory:
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:
Secondly, you have to create a module-info.java where you declare the required modules to run the application:
Next, you can compile the HelloWorldFX.java application with the JavaFX jmod files:
- for Linux/x64, macOS/x64, macOS/AArch64
- for Windows
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
- on Windows
After the image is built, you can run it using the following command:
- on Linux/x64, macOS/x64, macOS/AArch64
- on Windows
Final words
Congratulations on successfully downloading, installing, and using JavaFX!
For further reading, please check out JavaFX Fundamentals.
Last update: November 15, 2023