How to get JavaFX and Java 11 working in IntelliJ IDEA

旧街凉风 提交于 2019-11-29 07:53:04

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

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.

  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

  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.

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

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.

Click apply and close the dialog.

  1. Run the project

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

You could download a version of java which includes JavaFX from https://www.azul.com/downloads/zulu/zulufx/. In that case there's no special setup involved to run JavaFx applications on Java 11. JavaFX will work just like on Java 8.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!