IntelliJ 不会从 Maven 依赖项 (JavaFX 17) 加载 javafx 包 [英] IntelliJ doesn't load javafx packages from Maven dependencies (JavaFX 17)

查看:26
本文介绍了IntelliJ 不会从 Maven 依赖项 (JavaFX 17) 加载 javafx 包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让一个从 javafx-archetype-fxml 原型创建且未经编辑的 Maven/JavaFX 项目在最新版本的 IntelliJ 中运行.需要明确的是,该项目是该原型的直接副本;我只是想让一个例子起作用.

I'm trying to get a Maven/JavaFX project, created from the javafx-archetype-fxml archetype and unedited, to run in the latest version of IntelliJ. To be clear, the project is a direct copy of that archetype; I'm just trying to get an example working.

可以说我是 Maven 的完全初学者,所以我可能只是在这里遗漏了一个明显的步骤.

Suffice it to say I'm a complete beginner with Maven, so I'm could just be missing an obvious step here.

Maven 构建进展顺利,项目的 pom.xml 看起来就像 JavaFX 文档所说的那样.
除了更新 maven.compiler.sourcemaven.compiler.target 属性,以及 release 属性在maven-compiler-plugin,到16,我用于项目的JDK版本:

Maven build went smoothly, and the project's pom.xml looks the way the JavaFX documentation says it should.
I left it unchanged except for updating the maven.compiler.source and maven.compiler.target properties, as well as the release property in the maven-compiler-plugin, to 16, the JDK version I'm using for the project:

    <project xmlns="http://maven.apache.org/POM/4.0.0" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven- 
    v4_0_0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>com.epre</groupId>
        <artifactId>jfx-sandbox</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <maven.compiler.source>16</maven.compiler.source>
            <maven.compiler.target>16</maven.compiler.target>
        </properties>
        
        <dependencies>
            <dependency>
                <groupId>org.openjfx</groupId>
                <artifactId>javafx-controls</artifactId>
                <version>17</version>
            </dependency>
            <dependency>
                <groupId>org.openjfx</groupId>
                <artifactId>javafx-fxml</artifactId>
                <version>17</version>
            </dependency>
        </dependencies>
        
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.8.0</version>
                    <configuration>
                        <release>16</release>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.openjfx</groupId>
                    <artifactId>javafx-maven-plugin</artifactId>
                    <version>0.0.6</version>
                    <executions>
                        <execution>
                            <!-- Default configuration for running -->
                            <!-- Usage: mvn clean javafx:run -->
                            <id>default-cli</id>
                            <configuration>
                                <mainClass>com.epre.App</mainClass>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    
    </project>

依赖项显示在 Maven 选项卡中,我可以毫无问题地重新加载项目.同样,我可以看到 javafx-base-controls-fxml-graphics 和它们的相应的 :win 库已添加到项目的外部库 (pic):

The dependencies show up in the Maven tab, and I'm able to reload the project with no problems. Similarly, I can see that the javafx-base, -controls, -fxml, -graphics, and their corresponding :win libraries have been added to the project's External Libraries (pic):

然而,当我尝试运行项目的Main 类时,IntelliJ 抛出了大约 15 个错误,告诉我我试图从不导入的许多包不存在.

However, when I try to run the project's Main class, IntelliJ throws ~15 errors telling me that many of the packages I'm trying to import from don't exist.

    java: package javafx.application does not exist  
    java: package javafx.fxml does not exist  
    java: package javafx.scene does not exist  
    java: package javafx.scene does not exist  
    java: package javafx.stage does not exist  
    java: cannot find symbol class Application  
    java: cannot find symbol class Scene  
    java: method does not override or implement a method from a supertype  
    java: cannot find symbol class Stage  
    java: cannot find symbol class Scene  
    java: cannot find symbol class Parent  
    java: cannot find symbol class FXMLLoader  
    java: cannot find symbol class FXMLLoader  
    java: cannot find symbol method launch()

这是 Main 类,只是为了显示我试图从中导入的包类型:

This is the Main class, just to show what sort of packages I'm trying to import from:

    package com.epre;
    
    import javafx.application.Application;
    import javafx.fxml.FXMLLoader;
    import javafx.scene.Parent;
    import javafx.scene.Scene;
    import javafx.stage.Stage;
    
    import java.io.IOException;
    
    /**
     * JavaFX App
     */
    public class App extends Application {
    
        private static Scene scene;
    
        @Override
        public void start(Stage stage) throws IOException {
            scene = new Scene(loadFXML("primary"), 640, 480);
            stage.setScene(scene);
            stage.show();
        }
    
        static void setRoot(String fxml) throws IOException {
            scene.setRoot(loadFXML(fxml));
        }
    
        private static Parent loadFXML(String fxml) throws IOException {
            FXMLLoader fxmlLoader = new FXMLLoader(App.class.getResource(fxml + ".fxml"));
            return fxmlLoader.load();
        }
    
        public static void main(String[] args) {
            launch();
        }
    
    }

我已经做了一些搜索并尝试修复与项目无关的每个问题,只是为了尝试隔离问题,即

I've done some searching and tried fixing every unrelated problem with the project, just to try and isolate the issue, i.e.

  • 将项目语言级别更改为 16
  • 将项目模块的每模块字节码版本更改为 16
  • 用前面提到的 16" 替换 pom.xml 中的 11" 实例
  • 在旧版 IntelliJ 中打开项目
  • Changing the project language level to 16
  • Changing the project module's Per-module bytecode version to 16
  • Replacing instances of "11" in the pom.xml with "16" as mentioned earlier
  • Opening the project in an older version of IntelliJ

不过,这些都没有产生任何变化.

None of these have produced any change, though.

在以前也使用 JavaFX 的非 Maven 项目中,我必须将项目所需的所有包添加到它的 module-info.java.这是我能想到的唯一没有采取的步骤,因为我的理解是,如果我在 pom.xml?

In previous non-Maven projects that also used JavaFX, I had to add all the packages the project needed to its module-info.java. This is the only step I can think of that I haven't taken, since it's my understanding that I shouldn't have to deal with it if I'm declaring those packages as dependencies in the pom.xml?

我假设我不需要设置 module-info.java 因为 JavaFX 文档从未提到它是创建 Maven+JavaFK 项目的一个步骤:https://openjfx.io/openjfx-docs/.
JavaFX 和 IntelliJ >文档的非模块化 Maven 部分简单地指出,在加载项目时,JavaFX 类将被识别".

I'm assuming I don't need to set up a module-info.java because the JavaFX documentation never mentions it as a step for creating a Maven+JavaFK project: https://openjfx.io/openjfx-docs/.
The JavaFX and IntelliJ > Non-modular with Maven section of the documentation simply states that upon loading the project, "The JavaFX classes will be recognized."

编辑 2:我设法解决了包错误并使程序运行,方法是将 pom.xml 中的 JavaFX 依赖项更改为 版本 16 而不是 17.不确定为什么单个版本会像以前那样破坏程序,但我怀疑 JavaFX 的捆绑/分发方式可能发生了变化.

EDIT 2: I managed to solve the package errors and get the program running by changing the JavaFX dependencies in the pom.xml to version 16 instead of 17. Not sure why a single version would break the program like it did, though I suspect there was probably a change in how JavaFX is bundled/distributed.

推荐答案

JavaFX 17.0.0.1 版本更新

JavaFX 17.0.0.1 的发布已经解决了这个问题,当使用 JavaFX 和 Maven 时,应该使用这个版本而不是 17(它在 Maven 中仍然存在问题).

Update for JavaFX 17.0.0.1 release

The release of JavaFX 17.0.0.1 has resolved this issue and, when using JavaFX and Maven this is the version that should be used instead of 17 (which remains broken in Maven).

在使用 Maven 定义对 JavaFX 17 的依赖项时,确保为 JavaFX 依赖项定义的版本不是 17,并且至少是 17.0.0.1.这是 JavaFX 17.0.0.1 Maven 模块的工作依赖项定义示例.

When defining a dependency on JavaFX 17 using Maven, ensure that the version defined for JavaFX dependencies is not 17 and is at least 17.0.0.1. Here is an example of a working dependency definition for JavaFX 17.0.0.1 Maven modules.

<dependency>
    <groupId>org.openjfx</groupId>
    <artifactId>javafx-controls</artifactId>
    <version>17.0.0.1</version>
</dependency>

有关发行版本和内容以及当前最新发行版的版本号的信息可在 JavaFX 发行说明中找到托管在 gluon.

Info on release versions and contents and the version number for the current latest release is available in the JavaFX release notes hosted at gluon.

现在,我将保留原始答案的其余部分,其中讨论了一些背景信息,就像最初创建时一样.

For now, I will leave the rest of the original answer, which discusses some of the background information, as it was when it was originally created.

JavaFX 17 之前版本(例如 16)的 Maven 模块仍然可以继续正常运行.但是,如果您有能力为您的应用程序升级和使用 JavaFX 17.0.0.1 或更高版本,我建议您这样做.

Maven modules for JavaFX versions prior to 17 (e.g. 16), still continue to function without issue. However, if you have the ability to upgrade and use JavaFX 17.0.0.1 or higher for your application, I encourage this.

JavaFX 17 将作为 JavaFX 的稳定长期版本进行维护.它将保持稳定的功能集并获得错误和安全修复支持多年.

JavaFX 17 will be maintained as a stable long-term release of JavaFX. It will maintain a stable feature set and receive bug and security fix support for many years.

我能够重现这个问题.

这是一个已知问题,仅影响依赖于 Maven 中央存储库中当前可用的初始 JavaFX 17 版本工件的项目.

This is a known issue only affecting projects which rely on the initial JavaFX 17 release artifacts currently available in the Maven central repository.

查看相关问题:

openjfx-dev 邮件列表上的问题讨论:

Discussion of the issue on the openjfx-dev mailing list:

当前的一种解决方法是,如果您的应用程序依赖于来自 Maven 中心的 JavaFX 工件,请使用 JavaFX 16 而不是 JavaFX 17,直到此问题得到解决.

One current workaround is, if your application relies on JavaFX artifacts from Maven central, to use JavaFX 16 rather than JavaFX 17 until this issue is fixed.

由于这是 JavaFX 17 版本的一个非常关键的问题,我预计它可能会在不久的将来在 JavaFX 17 版本的更新中得到解决.

As this is quite a critical issue with the JavaFX 17 release, I would expect it will likely be addressed in an update to the JavaFX 17 release in the near future.

Mac OS (Catalina) 10.15.7

$ java -version
openjdk version "16.0.2" 2021-07-20
OpenJDK Runtime Environment Temurin-16.0.2+7 (build 16.0.2+7)
OpenJDK 64-Bit Server VM Temurin-16.0.2+7 (build 16.0.2+7, mixed mode, sharing)

IntelliJ IDEA 2021.2 (Ultimate Edition)
Build #IU-212.4746.92, built on July 27, 2021

复制步骤

  1. 在 Idea 中创建一个新的 JavaFX 项目

  • 选择新建|项目
  • 在左侧选项卡中选择 JavaFX.
  • 选择语言:Java,构建系统:Maven,项目 SDK:16
  • 选择下一步 ->完成
    1. 测试新项目.

    • 右键单击 HelloApplication 并选择运行HelloApplication.main()"
    • 应用程序应运行并显示带有Hello!"字样的 JavaFX 应用程序窗口.按钮.
      1. 更改 JavaFX 版本

      • 编辑pom.xml
      • 将 JavaFX 依赖版本从 16 更改为 17
      • maven-compiler-plugin 版本可以保持在 16.
      • 在 Maven 选项卡中,单击刷新图标以重新加载所有 Maven 项目"
        1. 测试更新的项目.

        • 右键单击HelloApplication并选择运行'HelloApplication.main()'

          现在执行将失败并显示错误消息:

          Execution will now fail with the error message:

          java: package javafx.fxml does not exist 
          

        • 这篇关于IntelliJ 不会从 Maven 依赖项 (JavaFX 17) 加载 javafx 包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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