Your JavaFX App on JDK 11 & OpenJFX
Introduction
With the advent of Java 11, there were several key changes. One of them is the disconnection of JavaFX from JDK. JavaFX is a technology for creating a graphical user interface that was created as a replacement for Swing and AWT. From Java 8 this is the recommended library for creating a GUI. After the release of version 11, you can download it independently. From the developer’s point of view, the most important thing is to include JavaFX in your project.
Another significant change is the release of two versions of Java:
- the one that can be downloaded from the official Oracle website - it can be used free of charge only for non-commercial purposes;
- OpenJDK to download at jdk.java.net - it can be used free of charge even for commercial purposes (GPL license).
These versions are practically identical. The difference is that OpenJDK has much shorter support (6 months from the release date) than the Oracle version (several years). If we want our version of Java to receive any security patches (in the case of commercial applications) for a longer period of time, we have to choose the first option with a monthly subscription fee.
OpenJDK Installation on Windows
- Download JDK as a zip file
- Extract and move it to any place
- Open “Control Panel” and then “System”
- Click “Advanced System Settings”
- Select “Advanced” and then “Environment Variables”
- Create new system variable
• Variable name: JAVA_HOME
• Variable value: installation path of the JDK e.g. C:\jdk-11.0.2 - Edit “Path” variable and add “%JAVA_HOME%\bin”
- Open “Command Prompt” and enter “java -version”
You should see something like that:
OpenJFX Installation on Windows
- Download “JavaFX Windows SDK”
- Extract to your specified folder
IDE Settings
When we download and install all the necessary files, we can take care of our application. We use this method when migrating application to the new version of JDK or creating a completely new project. The whole will be shown on the example of IntelliJ IDEA.
- Open an existing project or create a new one
- Set project SDK
File → Project Structure → Project → Project SDK
• Select the folder with the OpenJDK location
• Check if the “Project language level” is the same as the SDK version - Add OpenJFX as a library
File → Project Structure → Libraries → + → Java
• Select the folder with the javafx-sdk\lib location - Add a global path to JavaFX SDK
File → Settings → Appearance & Behavior → Path Variables → +
• Name: PATH_TO_FX
• Value: select the folder with the javafx-sdk\lib location - Set VM options
Run → Edit Configurations
• Paste the following text:--module-path ${PATH_TO_FX} --add-modules=javafx.controls,javafx.fxml
After completing the above steps, your application should work on OpenJDK & OpenJFX.
Leave a comment