Using JavaFX with IntelliJ

How to setup a development environment to build Java FX 11 applications with IntelliJ 2019.3.

Java FX was originally developed by Sun in 2008 as a rich multimedia development platform to compete with Adobe Flash. Over time, it evolved into a very capable and well-designed replacement for Java Swing (the original JDK UI platform).

However, in 2018, Oracle (the current owner and steward of Java) elected to remove Java FX from the standard JDK distribution. At the moment, it’s maintained by Gluon as an Open Source project Open JFX. Since JavaFX is no longer bundled with the JDK, there’s some additional steps required to setup your development environment.

In the following section I’ll detail the steps required to download, install and configure your development environment to build and run Java FX applications. We’ll focus on Java 11 and the corresponding Open JFX 11, since these are both LTS (long-term supported). Although I work on a Mac, these steps are generic enough to apply to Mac, Windows, Linux.

Install Software

You will need to install the following software:

  1. Java JDK 11: download and install from AdoptOpenJDK using the installer for your platform. Do NOT install from the Oracle site (for IP reasons).
  2. Java FX 11 Libraries: download from Open JFX and unzip to a known location where you want to store these libraries (e.g. /Users/javery/libs/javafx).
  3. IntelliJ 2019.3 Community. download and install from Jetbrains IntelliJ IDEA home page. The Ultimate version will work fine but needs to be licensed.

Setup your environment

I recommend setting up path variables for your JAVA and JFX installations. In my .zshrc I’ve added the following entries to track my installation directories and add the Java executables to my path (which enables java, jar etc. from the command-line).

export JAVA_HOME='/Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk/Contents/Home'
export JAVA_FX_HOME='/Users/javery/lib/javafx-sdk-11.0.2'
export PATH=$PATH:$JAVA_HOME/bin

This should work for any Unix shell (e.g. bash, zsh) in the appropriate config file. On Windows, you would want to add these as environment variables and update your path accordingly.

Setup IntelliJ

Out-of-the-box, IntelliJ won’t know about your Java 11 installation. To fix this:

  1. Launch IntelliJ, and from the Title Screen, click on Configure-Structure for New Projects.

  1. Under Platform-Settings, click on SDKs, then + and add a new Entry pointing to your Java 11 installation directory. Click OK to save it.

Setup an IntelliJ Project

Now we’ll setup an IntelliJ project to run the JavaFX equivilant of ‘Hello World’.

  1. Launch IntelliJ, and select Create New Project. Choose Java and press Next [Note: you can choose JavaFX as the project type, but it will create some empty classes that we don’t need].

  2. Accept default for all options. Name the project HelloFX and give it a location (e.g. /Users/javery/Desktop/HelloFX) and click Finish.

  3. Navigate to the src folder in the Project Explorer, and then click File-New-Java Class.

Copy-paste the following into your new file, and name it HelloFX.java:

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

// Revised HelloWorld application that uses the JavaFX base class
// The main method is optional in this case!

public class HelloFX extends Application {

    // The main method is optional with Application classes.
    // Use launch() to cause the other methods to be run in sequence.
    public static void main(String[] args) {
        launch();
    }

    @Override
    public void start(Stage stage) {
        System.out.println("Entering start method");
        // A stage is the application window automatically created by the framework
        // The scene holds the content to be displayed, which is stored as tree
        Label label = new Label("Hello JavaFX");
        Scene scene = new Scene(new StackPane(label), 640, 480);

        // We can have multiple scenes. Setup this one, and tell the stage to show it.
        stage.setScene(scene);
        stage.show();
    }

    @Override
    public void stop() throws Exception {
        super.stop();
        System.out.println("Entering init method");
    }
}

You will immediately see that IntelliJ “complains” about the code, by underlining what it considers to be errors. Don’t panic! The problem is that IntelliJ doesn’t include the JavaFX libraries, and it doesn’t know where to find them.

From the imports at the top of the java file, we can see that the code is referencing a number of JavaFX packages: javafx.application, javafx.scene and javafx.stage. If you look in your $JAVA_FX_HOME/lib directory (i.e. /lib where you installed JavaFX), you will see corresponding JAR files containing all of these libraries.

  • javafx-swt.jar
  • javafx.base.jar
  • javafx.controls.jar
  • javafx.fxml.jar
  • javafx.graphics.jar
  • javafx.media.jar
  • javafx.properties
  • javafx.swing.jar
  • javafx.web.jar

For this code to compile and run, we need to (a) make the libraries available to the compiler and (b) make them available when the application executes. We’ll fix both of these problems in IntelliJ.

Adding libraries

  1. Click on File-Project Structure, then click on Libraries.
  2. Click + and add details for your JavaFX installation. It should look something like this. Click OK.

Now all of the libraries are available to the compiler, and the errors should disappear! That’s the first problem solved.

Running the application

Running it is the next hurdle. If you run it (Run-Run from the menu), you will likely see an error like this:

Error: JavaFX runtime components are missing, and are required to run this application

To fix this, we need to modify the Run Configuration so that the Java Virtual Machine (JVM) can find the Java FX libraries at runtime.

  1. Click on Run-Edit Configurations. If you see an entry for HelloFX, go ahead and select it, otherwise click on the + sign and add a configuration for an Application. It should look like this:

  1. Add the following to the VM options, usingt the environment variable that we setup earlier to point to your JAVA_FX installation directory. Click OK when finished.

--module-path ${JAVA_FX_HOME}/lib --add-modules javafx.base,javafx.controls,javafx.fxml,javafx.graphics,javafx.media,javafx.web

  1. Click on the Run button (Run-Run from the menu) and it should launch.

Typically you would need to edit the Run configuration for each of your Java FX project file. You can also make this the default behaviour for any project that you run, so that the Java FX VM options are always used. To do this, click on Help-Edit Custom VM Options and add the VM options line (from above) to the bottom of the options list.

That’s it. Happy coding!

Related