IntelliJ 无法使用 OpenJDK 11 识别 JavaFX 11 [英] IntelliJ can't recognize JavaFX 11 with OpenJDK 11

查看:44
本文介绍了IntelliJ 无法使用 OpenJDK 11 识别 JavaFX 11的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法让 IntellJ 识别 JavaFX 包.使用新的 JavaFX 项目,使用 OpenJDK 11,尝试构建项目时,IntelliJ 无法识别 JavaFX 包.

我从 Maven 存储库中导入了 openjfx:javafx-base-11.

我查看了其他问题,解决方案似乎包括检查字节码是否处于正确级别(我的是),以及项目语言是否正确(我的是).

有人有什么想法吗?

错误:

解决方案

正如评论中提到的,

  1. 在运行默认项目之前,您只需要将这些添加到 VM 选项中:

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

  2. 运行

Maven

如果您使用 Maven 构建项目,请按照以下步骤操作:

  1. 使用 JavaFX 原型创建 Maven 项目
  2. 设置 JDK 11(指向您本地的 Java 11 版本)
  3. 添加 JavaFX 11 依赖项.

    <依赖><groupId>org.openjfx</groupId><artifactId>javafx-controls</artifactId><版本>11</版本></依赖><依赖><groupId>org.openjfx</groupId><artifactId>javafx-fxml</artifactId><版本>11</版本></依赖></依赖项>

执行此操作后,您会注意到现在可以在编辑器中识别 JavaFX 类.

您会注意到 Maven 为您管理所需的依赖项:它会为 javafx.controls 添加 javafx.base 和 javafx.graphics,但最重要的是,它会根据您的需求添加所需的分类器平台.就我而言,Mac.

这就是为什么你的 jars org.openjfx:javafx-controls:11empty,因为有三种可能的分类器(windows、linux 和 mac 平台),包含所有类和本机实现.

如果您仍想转到 .m2 存储库并手动从那里获取依赖项,请确保选择正确的(例如 .m2/repository/org/openjfx/javafx-controls/11/javafx-controls-11-mac.jar)

  1. 用来自 这里.

  2. 运行 mvn compile javafx:ru​​n,它应该可以工作了.

类似的方法也适用于 Gradle 项目,详见此处.

编辑

提到的入门指南包含更新的 文档 和 IntelliJ 的示例项目:

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?

Edit:

Error:

解决方案

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.

  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.

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:

这篇关于IntelliJ 无法使用 OpenJDK 11 识别 JavaFX 11的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆