The Practice and Philosophy of Object-Oriented Programming in Java

How to Set Up IntelliJ IDEA for Java and JavaFX Development


Step 1: SOFTWARE TO DOWNLOAD

You will need the following packages:

  1. The Java Development Kit. There are two major implementations: Oracle JDK and OpenJDK. The differ in terms of licensing and release schedules, but for personal use it really does not matter which implementation you choose. Just pick one and download/install the latest version (or see the note below about using an older version).
  2. IntelliJ IDEA (Community Edition). See the installation guide for OS-specific instructions if needed.
  3. Download JavaFX. This is a zipped folder named javafx-sdk (with version number appended). Unzip the folder and move it to a permanent location on your hard drive.

When you install the JDK, you will be prompted to create the home directory in a default location on your hard drive or to select another location — either way, make note of the location so that you can refer to it later. The following image shows the Windows installer saving the JDK to C:\Program Files\Java\jdk-14.0.1\.

Using an Older Version of the JDK

Java 8, released in 2014, includes all of the language and library features used in this book, so it is not technically necessary to have the most recent release. However, Oracle recommends uninstalling any versions prior to Java 11 for security reasons, noting also that the latest version will offer the newest feature updates, bug fixes, and performance improvements. If you do choose to use Java 8, you do not need to download JavaFX and you can skip the remaining steps below since JavaFX was part of the JDK 8. With the advent of Java 11 in 2018, however, JavaFX was removed from the JDK so that it could evolve at its own pace as an independent open-source project guided by Oracle and others in the OpenJFX community.


Step 2: Configuring IntelliJ IDEA for Java Development

After installing IntelliJ IDEA, you will need to configure it for Java development by specifying the location of the JDK home directory. To do this, start the IDE, click on the configure gear at the bottom of the welcome screen, and select Structure for New Projects:

The following dialog will appear:

Click the + icon to add a new JDK:

Enter or select the JDK home directory. In the example shown below, there are two versions available (13 and 14.0.1) and the latter is selected:

After selecting the JDK home directory, a list of class paths will automatically appear in the classpath tab:

Click the OK button. Your IDE is now configured with the JDK. However, for projects using JavaFX classes it will be necessary to perform one additional step.


Step 3: Adding JavaFX Libraries to your IntelliJ Project

If you create a new JavaFX application or open an existing one, you will notice compile-time errors in the editor: names of classes in the JavaFX API are not recognized. To fix this, you need to add the JavaFX libraries to the project. First, click on the File menu and select Project Structure. In the dialog that appears, select the Libraries tab and click the + icon to add a new Java library:

Find your javafx-sdk folder and select the lib subfolder:

Click the OK button to complete the process.

At this point, all compile-time errors have been resolved. If you try to run the application and a ClassNotFound exception is thrown, a fourth and final step, described next, will be needed.


Step 4: Editing the Project's Run Configuration

Note: As mentioned above, this step may not be necessary. Try to run a JavaFX application after completing the first three steps. If you succeed, there is no need to edit your project's run configuration.

From the run menu, select edit configurations. In the dialog that appears, you will see a field for VM options. You need to enter two options into this field with the following general form:

    --module-path %PATH_TO_FX%
    --add-modules=%FX_MODULES_NEEDED%

Instead of %PATH_TO_FX%, include the actual path to the JavaFX SDK lib directory. The path syntax will depend on your operating system. For the example illustrated in the preceding steps, the first VM option would be:

    --module-path "C:\Program Files\Java\javafx-sdk-11.0.2\lib"

The quote marks are necessary since the name of one of the directories in the path contains a space.

The JavaFX SDK is partitioned into seven modules. Include a comma-separated list of the ones your project will need in place of %FX_MODULES_NEEDED%. For example:

    --add-modules=javafx.base,javafx.controls,javafx.graphics,javafx.media

Later you can add other modules if you need to, or remove some, by editing your run configuration again.

If the application does not run at this point, close and restart the IDE.

For troubleshooting information, consult the JetBrains help page.


Return to Companion Site home page