Intellij Can't Recognize Javafx 11 With Openjdk 11

IntelliJ can't recognize JavaFX 11 with OpenJDK 11

As mentioned in the comments, the Starting Guide is the place to start with Java 11 and JavaFX 11.

The key to work as you did before Java 11 is to understand that:

  • JavaFX 11 is not part of the JDK anymore
  • You can get it in different flavors, either as an SDK or as
    regular dependencies (maven/gradle).
  • You will need to include it to the module path of your project, even if your project is not modular.

JavaFX project

If you create a regular JavaFX default project in IntelliJ (without Maven or Gradle) I'd suggest you download the SDK from here. Note that there are jmods as well, but for a non modular project the SDK is preferred.

These are the easy steps to run the default project:

  1. Create a JavaFX project
  2. Set JDK 11 (point to your local Java 11 version)
  3. Add the JavaFX 11 SDK as a library. The URL could be something like /Users/<user>/Downloads/javafx-sdk-11/lib/. Once you do this you will notice that the JavaFX classes are now recognized in the editor.

JavaFX 11 Project


  1. Before you run the default project, you just need to add these to the VM options:

    --module-path /Users/<user>/Downloads/javafx-sdk-11/lib --add-modules=javafx.controls,javafx.fxml

  2. Run

Maven

If you use Maven to build your project, follow these steps:

  1. Create a Maven project with JavaFX archetype
  2. Set JDK 11 (point to your local Java 11 version)
  3. Add the JavaFX 11 dependencies.

    <dependencies>
    <dependency>
    <groupId>org.openjfx</groupId>
    <artifactId>javafx-controls</artifactId>
    <version>11</version>
    </dependency>
    <dependency>
    <groupId>org.openjfx</groupId>
    <artifactId>javafx-fxml</artifactId>
    <version>11</version>
    </dependency>
    </dependencies>

Once you do this you will notice that the JavaFX classes are now recognized in the editor.

JavaFX 11 Maven project

You will notice that Maven manages the required dependencies for you: it will add javafx.base and javafx.graphics for javafx.controls, but most important, it will add the required classifier based on your platform. In my case, Mac.

This is why your jars org.openjfx:javafx-controls:11 are empty, because there are three possible classifiers (windows, linux and mac platforms), that contain all the classes and the native implementation.

In case you still want to go to your .m2 repo and take the dependencies from there manually, make sure you pick the right one (for instance .m2/repository/org/openjfx/javafx-controls/11/javafx-controls-11-mac.jar)


  1. Replace default maven plugins with those from here.

  2. Run mvn compile javafx:run, and it should work.

Similar works as well for Gradle projects, as explained in detail here.

EDIT

The mentioned Getting Started guide contains updated documentation and sample projects for IntelliJ:

  • JavaFX 11 without Maven/Gradle, see non-modular sample or modular sample projects.

  • JavaFX 11 with Maven, see non-modular sample or modular sample projects.

  • JavaFX 11 with Gradle, see non-modular sample or modular sample projects.

How to get JavaFX and Java 11 working in IntelliJ IDEA

Download the appropriate JavaFX SDK for your operating system and unzip it to a desired location, for instance /Users/your-user/Downloads/javafx-sdk-11.

  1. Create a JavaFX project

Sample Image

Create a JavaFX project Provide a name to the project, like HelloFX, and a location. When the project opens, the JavaFX classes are not recognized.
Sample Image


  1. Set JDK 11

Go to File -> Project Structure -> Project, and set the project SDK to 11. You can also set the language level to 11. Set JDK 11
Sample Image


  1. Create a library

Go to File -> Project Structure -> Libraries and add the JavaFX 11 SDK as a library to the project. Point to the lib folder of the JavaFX SDK.
Sample Image

Once the library is applied, the JavaFX classes will be recognized by the IDE.
Sample Image

Warning: If you run now the project it will compile but you will get this error:

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

This error is shown since the Java 11 launcher checks if the main class extends javafx.application.Application. If that is the case, it is required to have the javafx.graphics module on the module-path.


  1. Add VM options

To solve the issue, click on Run -> Edit Configurations... and add these VM options:

--module-path %PATH_TO_FX% --add-modules=javafx.controls,javafx.fxml

Note that the default project created by IntelliJ uses FXML, so javafx.fxml is required along with javafx.controls. If your project uses other modules, you will need to add them as well.
Sample Image
Click apply and close the dialog.


  1. Run the project

Click Run -> Run... to run the project, now it should work fine.

Detailed resource: https://www.jetbrains.com/help/idea/javafx.html

cannot resolve symbol javafx.application in IntelliJ Idea IDE

As indicated here, JavaFX is no longer included in openjdk.

So check, if you have <Java SDK root>/jre/lib/ext/jfxrt.jar on your classpath under Project Structure -> SDKs -> 1.x -> Classpath? If not, that could be why. Try adding it and see if that fixes your issue, e.g. on Ubuntu, install then openjfx package with sudo apt-get install openjfx.

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

There are similar questions like this or this other one.

Before JavaFX 11, whenever you were calling something JavaFX related, you had all the javafx modules available within the SDK.

But now you have to include the modules/dependencies you need.

Your error says that you are using FXML but it can't be resolved, but you have just added the javafx.controls module:

--add-modules=javafx.controls

As you can see in the JavaDoc the javafx.controls module depends on javafx.graphics and java.base, but none of those modules includes the FXML classes.

If you need FXML classes like the FXMLLoader, you need to include javafx.fxml module:

 --module-path="C:\Program Files\Java\javafx-sdk-11\lib" \
--add-modules=javafx.controls,javafx.fxml

The same will apply if you need media or webkit, those have their own modules.

How to open JavaFX .jar file with JDK 11?

Providing you have a simple (non-modular) JavaFX 11 project (without Maven/Gradle build tools), and you are using IntelliJ, like the HelloFX sample from here,
this is how you can create a jar from IntelliJ that can be run from the console

A full tutorial on how to run the project can be found here, and instructions on how to create a jar are here (see section Non-modular project), but these doesn't cover Artifacts from IntelliJ.

Check that the HelloFX project runs from IntelliJ with these VM options:

--module-path ${PATH_TO_FX} --add-modules javafx.controls,javafx.fxml

where PATH_TO_FX has been set in File -> Settings -> Appearance & Behavior -> Path Variables, pointing to the JavaFX SDK lib.

Semi fat Jar

We can create a Jar that only contains the classes from the project, and third party dependencies, but not JavaFX ones.

Go to File -> Project Structure -> Artifacts -> Add -> JAR -> From modules with dependencies, add your main class, accept.

Then remove the JavaFX jars from the list, and accept.

SemiJar

Build the project, it will create a quite small jar (3 KB in this case).

Now you should be able to run it like:

java --module-path %PATH_TO_FX% --add-modules javafx.controls,javafx.fxml -jar out\artifacts\HelloFX_jar\HelloFX.jar

(make sure that %PATH_TO_FX% points to a valid folder and use quotes if it contains spaces.

You can distribute this jar, and run it in other platforms, providing those also have the JavaFX SDK.

Fat Jar

If you want a full fat jar that includes JavaFX dependencies, you can still use Artifacts.

Go to File -> Project Structure -> Artifacts -> Add -> JAR -> From modules with dependencies, add your main class, accept.

Then keep the JavaFX jars from the list, and accept. Build the project.

In theory, you should be able to run it like:

java -jar out\artifacts\HelloFX_jar\HelloFX.jar

But this won't work.

Reason 1: You need a launcher class, as explained here.

So create a launcher class:

public class Launcher {

public static void main(String[] args) {
Main.main(args);
}
}

Reason 2: If you only add your SDK jars to the fat jar, you will be missing the native libraries, as explained here.

So edit the artifact, select the Launcher class as main class, and add the native libraries (Directory Content -> path-to/JavaFX SDK/bin on Windows):

Fat Jar

Now build the project (now the jar is about 33 MB, and contains unnecessary native libraries) and run:

java -jar out\artifacts\HelloFX_jar\HelloFX.jar

You can distribute this jar, but only to Windows platforms.

You can create similar jars for other platforms, if you download their JavaFX SDKs, and you can also build cross-platform jars if you add them all together, as explained in the linked answers above.

Anyway, you should consider using jlink instead.

Note

About this error:

Caused by: java.lang.ClassNotFoundException: Files\Java\javafx-sdk-11.0.1\lib

it looks like the library path was set without quotes and it is missing the first part of the path C:\Program Files\.... Just make sure you use quotes:

set PATH_TO_FX="C:\Program Files\Java\javafx-sdk-11.0.1\lib"


Related Topics



Leave a reply



Submit