How to Set Up JavaFX in IntelliJ

This guide assumes that you have already installed Java and IntelliJ IDEA. It also assume that you are working in Windows. The same basic ideas apply in Linux or on a Mac, but some details will differ.


Step 1: Download JavaFX.

Download the JavaFX SDK. This is a zipped folder named javafx-sdk with version number appended. In file explorer, navigate to C:\Program Files and create a new folder there named JavaFX. Move the file that you downloaded into this new folder and unzip it.


Step 2: Add JavaFX to your project.

If you want to create a JavaFX application in an IntelliJ project, you will need to add the JavaFX library. 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. The path shown in the screenshot below is just an example. Look for the path to the lib subfolder on your computer, or use:

Click the OK button to complete the process.

Now right-click on a package and select new. Select the option to create a new JavaFX application:

A code template will appear in the editor. Complete the start method with the instructions that you would like to execute, then try to run the application. You will see that an exception is thrown.

The fourth and final step of the set-up process, described next, will fix this problem.


Step 3: Editing the Project's Run Configuration

In the menu bar at the top you will see a green debug icon. Next to it is an action menu indicated by a vertical ellipsis. From the action menu, select Run with Parameters. (If you do not see this option, you might instead see Edit—this will work).

In the dialog that appears, click on Modify Options and then select Add VM Options. Copy the following text into the VM options field:

--module-path "C:\Program Files\JavaFX\javafx-sdx-22.0.2\lib"
--add-modules=javafx.base,javafx.controls,javafx.graphics,javafx.media

Note: the module path shown above is correct for the computers in our classroom. For computers in Ben Franklin, modify the path as discussed in Step 2. If you are working on your own computer, the path to lib will depend on where you installed JavaFX. Note also that in Windows, you need the double quotes around the path as shown above.

You can now run the application.


Final Note

You will need to add the VM options for every JavaFX application in the current project. However, you can configure IntelliJ to automatically include the VM options for all future projects as follows. From the top menu bar, select FileNew Projects SetupRun Configurations Template. In the pop-up window that appears, select ApplicationModify optionsAdd VM options and enter the options as described above.