Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IntelliJ can't recognize JavaFX 11 with OpenJDK 11

I'm having trouble getting IntellJ to recognize JavaFX packages. With a new JavaFX project, with OpenJDK 11, when trying to build the project, IntelliJ can't recognize the JavaFX packages.

I've imported openjfx:javafx-base-11 from the Maven repo.

I've looked at other questions and the solutions seem to range from checking that the bytecode is at the right level (mine is), and that the project language is correct (mine is).

Anyone have any ideas?

pic 1

pic 2

pic 3

Edit:

Error:

enter image description here

like image 564
AlwaysNeedingHelp Avatar asked Sep 23 '18 15:09

AlwaysNeedingHelp


People also ask

Does OpenJDK 11 support JavaFX?

JavaFX 11 does crash with OpenJDK 11 on Ubuntu 18.04 Linux PCs with the Wayland window server enabled. Use of the Xorg server is recommended as a workaround.

Why JavaFX is not working in IntelliJ?

To be able to work with JavaFX in IntelliJ IDEA, the JavaFX bundled plugin must be enabled: In the Settings/Preferences dialog ( Ctrl+Alt+S ), select Plugins. Switch to the Installed tab and make sure that the JavaFX plugin is enabled. If the plugin is disabled, select the checkbox next to it.

Does JavaFX work with OpenJDK?

This means that JavaFX can be used from any modern JDK running on a supported platform, by including the appropriate JavaFX modules (available from openjfx or Maven Central) on the runtime module path. All JavaFX modules available from openjfx are open source implementations that contain no closed source code.

Does Java 11 support JavaFX?

In Java 11, JavaFX was removed from the SDK. It is now in its own separate module, and if you want to use it in your application you will need to specifically include it.


1 Answers

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.

like image 114
José Pereda Avatar answered Oct 18 '22 20:10

José Pereda