[Solved] cannot resolve symbol javafx.application in IntelliJ Idea IDE – Java

Photo of author
Written By M Ibrahim
intellij-idea javafx

Quick Fix: OpenJDK no longer includes JavaFX by default. Check if jfxrt.jar is on your classpath and add it if it is not. For Ubuntu, install the openjfx package via sudo apt-get install openjfx. Refer for more details.

The Problem:

A developer using IntelliJ Idea IDE with JavaFX encounters a compilation error stating "java: package javafx.application does not exist." Despite changing Project SDK, Project Language Level to Java 8, reloading the project, and enabling the JavaFX plugin, the issue persists. The developer seeks assistance in resolving this error.

The Solutions:

Solution 1: Install OpenJFX Package

OpenJDK no longer includes JavaFX by default. To resolve this issue, follow these steps:

1. Check if you have `<Java SDK root>/jre/lib/ext/jfxrt.jar` on your classpath under `Project Structure -> SDKs -> 1.x -> Classpath`. Add it if it isn’t already there.

2. In some Linux distributions, like Ubuntu, you can install the OpenJFX package using the command
`sudo apt-get install openjfx`. This will add the necessary JavaFX libraries to your system.

3. After installing OpenJFX, verify that the `jfxrt.jar` file is located in `<Java SDK root>/jre/lib/ext/`. If not, add it manually.

4. Rebuild your project in IntelliJ Idea and check if the compile error persists.

Solution 2: The Perfect Solution

To resolve the "javafx.application" error in IntelliJ Idea IDE, follow these steps:

Step 1: Install Java 8 and OpenJDK 8

  1. Update your system with the latest packages:
sudo apt-get update
  1. Install Java 8:
sudo apt-get install openjdk-8-jre
  1. Install OpenJDK 8:
sudo apt-get install openjfx

Step 2: Copy JavaFX Libraries

  1. Copy the JavaFX JARs from OpenJDK 8 to Java 8:
sudo cp /usr/share/java/openjfx/jre/lib/ext/* /usr/lib/jvm/java-1.8.0-openjdk-amd64/lib
sudo cp /usr/share/java/openjfx/lib/* /usr/lib/jvm/java-1.8.0-openjdk-amd64/lib

Step 3: Grant Permissions

  1. Grant read and write permissions to the Java 8 directory:
sudo chmod 777 -R /usr/lib/jvm/java-1.8.0-openjdk-amd64

Step 4: Rebuild the Project

  1. Open IntelliJ Idea and rebuild your project. The "javafx.application" error should be resolved.

Solution 3: Using the IntelliJ’s JavaFX Library

If you are getting compile errors related to JavaFX in IntelliJ Idea, you can use the JavaFX library that comes with IntelliJ itself. Here’s how:

  1. Navigate to the IntelliJ installation directory on your computer. The default location is:

    • Windows: C:\Program Files\JetBrains\IntelliJ IDEA <version>
    • Mac: /Applications/IntelliJ IDEA.app/Contents
    • Linux: /opt/intellij/idea
  2. Find the folder named jre64. This folder contains the Java Runtime Environment (JRE) that IntelliJ uses.

  3. Inside the jre64 folder, locate the lib folder.

  4. In the lib folder, find a jar file named jfxrt.jar. This is the JavaFX runtime library.

  5. Copy the jfxrt.jar file to your project’s lib folder. If you don’t have a lib folder in your project, create one.

  6. Right-click on your project in IntelliJ Idea and select Open Module Settings.

  7. In the Module Settings dialog, select the Dependencies tab.

  8. Click the + icon and select JARs or directories….

  9. Navigate to the lib folder in your project and select the jfxrt.jar file.

  10. Click OK to add the JavaFX library to your project’s dependencies.

  11. Rebuild your project to ensure that the JavaFX library is properly integrated.

Now, you should be able to import and use JavaFX classes and APIs in your project without encountering compilation errors related to missing JavaFX symbols.

Solution 4: Download the JavaFX package and fix the classpath

  1. Download and install the java-openjfx package from the official Arch Linux repositories. Ensure that you also have the openjdk8-openjdk package installed.

  2. Open your project in Intellij and navigate to Project Structure > SDKs > 1.8 > Classpath.

  3. Remove the old JDK you had and click on the directory for the new JDK that now contains jfxrt.jar.

  4. Click OK to save the changes and rebuild your project.

This should resolve the javafx.application not recognized issue and allow you to use JavaFX in your IntelliJ Idea project.

Solution 5: Open Module Settings and Add jfxrt plugin to Classpath

To resolve the “cannot resolve symbol javafx.application” error in IntelliJ IDEA, you can update the module settings and manually add the jfxrt plugin path to the classpath:

  1. In the Project Structure window, left-click on the project folder and select "Open Module Settings" from the list of actions.
  2. Then, under the "Platform Settings" menu title, select "SDKs".
  3. In the "Classpath" list, check if you can find the jfxrt plugin path. If not, click on the "+" symbol on the right and select the directory of the jfxrt plugin (typically located at C:\Program Files\Java\jdk1.8.0\jre\lib\ext\jfxrt.jar on Windows systems).
  4. Once you have added the jfxrt plugin to the classpath, click "Apply" and then "OK" to save the changes.
  5. Rebuild the project to ensure that the necessary libraries are included and the error is resolved.