【问题标题】:Package 'com.example' reads package 'javafx.beans' from both 'javafx.base' and 'javafx.base'包“com.example”从“javafx.base”和“javafx.base”中读取包“javafx.beans”
【发布时间】:2019-05-02 01:14:57
【问题描述】:

module-info.java 我得到错误

包“com.example”从“javafx.base”和“javafx.base”中读取包“javafx.beans”。

迁移(Java 8 到 Java 11)不仅缓慢但肯定地让我感到沮丧,而且这个错误对我来说没有任何意义。

build.gradle 的依赖部分:

def springFrameworkVersion = '5.1.2.RELEASE'
def hibernateVersion = '5.3.7.Final'
def junitJupiterVersion = '5.3.1'

dependencies {
  compile 'org.transentials:cardhouse-commons:1.1.1'
  compile 'ch.qos.logback:logback-classic:1.2.3'
  compile "org.springframework:spring-context:$springFrameworkVersion"
  compile "org.springframework:spring-jdbc:$springFrameworkVersion"
  compile "org.springframework:spring-orm:$springFrameworkVersion"
  compile "org.hibernate:hibernate-core:$hibernateVersion"
  compile 'org.apache.commons:commons-dbcp2:2.5.0'
  compile 'org.apache.commons:commons-lang3:3.8.1'
  compile 'commons-io:commons-io:2.6'
  compile 'com.h2database:h2:1.4.197'
  compile 'javax.xml.bind:jaxb-api:2.3.1'
  compile 'com.google.guava:guava:27.0-jre'
  compile 'org.flywaydb:flyway-core:5.2.1'
  compile 'javax.validation:validation-api:2.0.1.Final'
  compile "org.openjfx:javafx-base:11:$platform"
  compile "org.openjfx:javafx-graphics:11:$platform"
  compile "org.openjfx:javafx-controls:11:$platform"
  compile "org.openjfx:javafx-fxml:11:$platform"
  testCompile 'junit:junit:4.12'

  testCompile 'org.mockito:mockito-core:2.+'
  testCompile 'de.saxsys:jfx-testrunner:1.2'
  testCompile 'org.apache.commons:commons-text:1.6'
  testCompile "org.junit.jupiter:junit-jupiter-api:$junitJupiterVersion"
  testCompile "org.junit.jupiter:junit-jupiter-engine:$junitJupiterVersion"
  testCompile 'org.hamcrest:hamcrest-all:1.3'
}

还有module-info.java

module open.terms.client.jfx {
  requires org.transentials.cardhouse.commons;
  requires com.google.common;
  requires org.apache.commons.lang3;
  requires org.hibernate.orm.core;
  requires java.persistence;
  requires slf4j.api;
  requires javafx.graphics;
  requires javafx.fxml;
  requires java.desktop;
}

有人可以向我解释一下编译器想通过这个告诉我什么吗?

【问题讨论】:

    标签: java java-module java-11 java-platform-module-system javafx-11


    【解决方案1】:

    使用所需的依赖列表,如果您从module-info 中删除所有所需的模块,IDE 仍然会报同样的错误:

    模块“”从“javafx.base”和“javafx.base”读取包“javafx.beans”

    所以问题不在于您的模块信息,而在于您的依赖项。如果您将它们全部注释掉,JavaFX 除外,问题就消失了。

    这意味着某些依赖项携带了一些不必要的 JavaFX 依赖项。

    我已经设法通过仅注释第一个依赖项来隔离问题:

    compile 'org.transentials:cardhouse-commons:1.1.1'
    

    所以问题是为什么会发生这种情况以及我们如何解决它。

    如果您访问 Maven Central repo,它会显示依赖项的 GitHub repo,您可以在其中找到 build.gradle 文件及其 module-info

    正如所料,它usesJavaFX:

    compile "org.openjfx:javafx-base:11:$platform"
    

    它还在其module-info 中提供requires javafx.base

    当您将这个工件与您的依赖项一起使用时,您正在导入它们的 javafx.base 导入,以及您从 JavaFX 依赖项中导入的,并且存在冲突。

    解决问题的最快方法就是在您的构建中更改它:

    compile 'org.transentials:cardhouse-commons:1.1.1'
    

    到这里:

    compile ('org.transentials:cardhouse-commons:1.1.1') {
        exclude group: 'org.openjfx'
    }
    

    因此您将排除它的 JavaFX 依赖项并使用您的。

    更永久的修复是将工件 org.transentials:cardhouse-commons 的模块信息更改为:

    `requires transitive javafx.base`
    

    您可以阅读有关transitive here 的使用。

    应该向作者报告问题。

    注意

    顺便说一句,您可以使用 javafx gradle plugin 来处理构建的所有相关 JavaFX 部分,将其简化为:

    plugins {
        id 'application'
        id 'org.openjfx.javafxplugin' version '0.0.5'
    }
    
    repositories {
        mavenCentral()
    }
    
    dependencies {
        compile ('org.transentials:cardhouse-commons:1.1.1') {
            exclude group: 'org.openjfx'
        }
        compile files('libs/cardhouse-commons-master-1.1.1.jar')
        ...
        compile 'javax.validation:validation-api:2.0.1.Final'
    }
    
    javafx {
        modules = [ 'javafx.controls', 'javafx.fxml' ]
    }
    
    mainClassName = 'open.terms.client.jfx.Main'
    

    OpenJFX 文档已经使用了这个插件。

    【讨论】:

    • 差不多,但我不同意您的“跨平台修复”。我宁愿根本不导出 javafx 依赖项。
    • 嗯,实际代码在gradle中,由central生成的pom.xml有点不同。我不建议将javafx.base 作为transitive 依赖项引入,而是使用文档中更新的插件来解决它。也许这个改变会帮助github.com/transentials/cardhouse-commons/pull/1/files
    • 问题还是一样。该插件不会修改模块信息,因此您将导出与以前完全相同的工件。我用transitive 修改了项目,这对我有用。
    • @nullpointer 你可以看到case:它是一个需要JavaFX的库,但它不会将其导出为依赖项,因此该库可以作为常规依赖项添加到其他最终应用程序中。
    • 使用跨平台解决方案适用于您希望通过胖罐分发的最终项目。虽然您应该改用 jlink 或 jpackager(如果可用),但它们当然是特定于平台的。就库而言,我不喜欢与它们一起使用 JavaFX,使用它们的最终项目毕竟将是一个 JavaFX 项目,并且它本身将包含这些依赖项。但是我看到其他一些项目没有意识到这个问题并在最终用户中产生了这种问题。
    【解决方案2】:

    错误显示,您最终在 JavaFX 的模块路径中放置了两次相同的模块

    您可能同时将 jmods 用于 OpenJFX 以及 OpenJFX SDK/lib 放在您的模块路径中。

    JavaFX 11 运行时可用作

    • 特定于平台的 SDK

    • 作为一些 jmod 和

    • 作为 maven Central 中的一组工件。

    这三个中的任何一个(一个)应该足以进一步使用,具体取决于您计划如何构建应用程序 - 模块化非模块化。

    编辑 1 [概念改进]

    在你的build.gradle 中,你应该只需要依赖于

    compile "org.openjfx:javafx-controls:11:$platform"
    compile "org.openjfx:javafx-fxml:11:$platform"
    

    由于模块,javafx.basejavafx.graphics 无论如何都会通过 javafx-controls 传递到模块路径中。此外,您必须确保,鉴于这些依赖关系,您不会在 Project Settings > Libraries 下添加任何库。

    编辑 2 [可扩展的改进]

    按照documentation at OpenJFX,你可以利用插件摆脱openjfx依赖

    plugins {
        id 'application'
        id 'org.openjfx.javafxplugin' version '0.0.5'
    }
    
    javafx {
        modules = [ 'javafx.controls', 'javafx.fxml' ]
    }
    
    dependencies {
        // all dependencies except openjfx
    }
    

    编辑 3 [动手]

    你的例子中的真正罪魁祸首是依赖

    compile 'org.transentials:cardhouse-commons:1.1.1'
    

    禁用此功能可解决此问题。您可能希望将它提升给库所有者(或者如果您拥有它,则修复此问题)以确保它不会带来 javafx.base 模块。确切地说,这种依赖关系将 org.openjfx:javafx-base:linux:11.0.1 作为依赖项引入,原因在它们的 pom.xml 依赖项中很清楚。

    【讨论】:

    • 我只使用 Maven Central 中的工件,没有 SDK,也没有 jmod。
    • @Hannes 在这种情况下,您能否用有效的 pom.xml 内容执行/编译项目来更新问题?
    • @Hannes 也更新了答案,您应该确保项目设置下没有库。
    • module-info.java 中删除 javafx.base 解决了这个问题 - 现在我只剩下那些烦人的 拆分包 问题了。
    • @nullpointer 关于您的编辑:您需要所有模块,当您拥有 分类器 时,传递依赖关系不会被解析(使用 Gradle)。
    猜你喜欢
    • 2019-04-13
    • 2021-09-14
    • 1970-01-01
    • 2022-01-26
    • 1970-01-01
    • 2021-01-04
    • 1970-01-01
    • 2021-10-16
    • 1970-01-01
    相关资源
    最近更新 更多