【问题标题】:Running javafx application - Error occurred during initialization of boot layer: java.lang.module.ResolutionException运行 javafx 应用程序 - 启动层初始化期间发生错误:java.lang.module.ResolutionException
【发布时间】:2020-10-03 21:12:39
【问题描述】:

正如标题所说,我只是想运行 javafx 应用程序。但是我不知道为什么会出现此错误,也不知道为什么错误引用了sisu.inject.beanaopalliance 模块。我不知道是 module-info.java 还是 pom.xml 导致了错误。提前感谢您的任何回答。

模块信息.java:

module ui {
    requires javafx.fxml;
    requires transitive javafx.graphics;
    requires javafx.controls;
    requires org.testfx.junit5;
    requires junit;
    requires core;
    exports ui.java;
    exports test.ui to junit;
}

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>

<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/xsd/maven-4.0.0.xsd">

    <parent>
        <groupId>it1901.nachwithme</groupId>
        <artifactId>parent</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <modelVersion>4.0.0</modelVersion>
    <artifactId>ui</artifactId>

    <dependencies>
        <!-- UI imports core as a dependency -->
        <dependency>
            <groupId>it1901.nachwithme</groupId>
            <artifactId>core</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <!-- Dependencies for javafx -->
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-fxml</artifactId>
        </dependency>
        <dependency>
            <groupId>org.testfx</groupId>
            <artifactId>testfx-core</artifactId>
            <exclusions>
                <exclusion>
                    <!-- https://mvnrepository.com/artifact/org.sonatype.sisu/sisu-inject-bean -->
                    <groupId>org.sonatype.sisu</groupId>
                    <artifactId>sisu-inject-bean</artifactId>
                </exclusion>
                <exclusion>
                    <!-- https://mvnrepository.com/artifact/aopalliance/aopalliance -->
                    <groupId>aopalliance</groupId>
                    <artifactId>aopalliance</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!-- Dependencies for javafx -->

        <!-- Dependencies for unit-testing -->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-graphics</artifactId>
            <version>16-ea+2</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.testfx</groupId>
            <artifactId>testfx-junit5</artifactId>
            <scope>compile</scope>
        </dependency>
        <!-- Dependencies for unit-testing -->
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>report</id>
                        <phase>test</phase>
                        <goals>
                            <goal>report</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-checkstyle-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>com.github.spotbugs</groupId>
                <artifactId>spotbugs-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.openjfx</groupId>
                <artifactId>javafx-maven-plugin</artifactId>
                <configuration>
                    <options>
                        <!-- <option>dash dash enable-preview</option> -->
                    </options>
                    <mainClass>ui.java.App</mainClass>
                </configuration>
            </plugin>
        </plugins>
        <sourceDirectory>src</sourceDirectory>
        <resources>
            <resource>
                <directory>src</directory>
                <includes>
                    <include>**/*.fxml</include>
                    <include>**/*.png</include>
                </includes>
            </resource>
        </resources>
        <testSourceDirectory>src/test</testSourceDirectory>
        <testResources>
            <testResource>
                <directory>test</directory>
                <includes>
                    <include>**/*.fxml</include>
                    <include>**/*.png</include>
                </includes>
            </testResource>
        </testResources>
    </build>
</project>

错误:

Error occurred during initialization of boot layer  
java.lang.module.ResolutionException: Modules sisu.inject.bean and aopalliance export package org.aopalliance.aop to module org.testfx

Process finished with exit code 1

【问题讨论】:

  • 起初我没有看到您的“我只是想运行 javafx 应用程序”。匆忙中,我最初的解决方案错误地认为您报告了编译错误。因此它提出了 maven-compiler-plugin 的配置。我现在使用 javafx-maven-plugin 的建议配置更正了解决方案。

标签: java maven javafx pom.xml module-info


【解决方案1】:

TL;DR — 这两个模块有一个共同的 split package。并且 JPMS 禁止拆分包。


冗长的版本

this 2010 blog第一句总结了什么是拆分包……1

...“拆分包”是一个非常古老的 Java 术语,您可以在不同的库中拥有相同名称的包,提供相关(或有时不相关的功能)...

请记住,这是 2010 年写的;在 JPMS 之前。因此,该博客在 第二句 中声称“在编译和运行时都没有任何问题”已经过时了;无论如何,对于模块路径。

如需更详细、最新的解释,请观看 JavaOne 2016 中的 Project Jigsaw: Under the Hood video2

...为什么错误引用了 sisu.inject.beanaopalliance 模块...

因为您的项目有一个模块描述符 (module-info.java) Maven 已将这些依赖项(我可以看到您在 pom 中声明)放在模块路径上.

...但是我不知道为什么会出现此错误...

因为 JPMS 讨厌拆分包……3

拆分包有两个问题

  • 如果你在包的每个部分都有相同的类,你的行为 [程序] 取决于类路径中的顺序, 我在两个不同的库中遇到过这种错误 不同版本的 ASM,在运行时,旧版本的一个类正在调用 新版本的类:(
  • 安全,如果你允许拆分包,你就允许任何人在任何包中插入任何类。

解决方案

…
<plugin>
    <groupId>org.openjfx</groupId>
    <artifactId>javafx-maven-plugin</artifactId>
    <version>0.0.4</version>
    <configuration>
        <options>
            <option>--patch-module</option>
            <option>org.sonatype.sisu=${env.M2_REPO}/aopalliance/aopalliance/1.0/aopalliance-1.0jar</option>
        </options>
        <mainClass>ui.java.App</mainClass>
    </configuration>
</plugin>
…






1 该博客谈论的是 OSGi。但核心拆分包定义也适用于 JPMS。
2 Project Jigsaw: Under The Hood — accompanying slides.
3 Rémi Forax——拆分包问题——Jigsaw 开发者邮件列表——2016 年 11 月。

【讨论】:

    猜你喜欢
    • 2020-06-24
    • 2019-02-24
    • 2021-05-17
    • 2021-10-03
    • 1970-01-01
    • 1970-01-01
    • 2021-07-23
    • 2021-03-02
    • 1970-01-01
    相关资源
    最近更新 更多