运行javafx应用程序 - 启动层初始化期间发生错误:java.lang.module.ResolutionException

问题描述 投票:0回答:2

正如标题所示,我只是想运行 javafx 应用程序。但是我不知道为什么会收到此错误,也不知道为什么错误引用

sisu.inject.bean
aopalliance
模块。我不知道 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
java maven javafx pom.xml module-info
2个回答
1
投票

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


冗长版本

2010 年博客第一句话总结了什么是拆分包……1

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

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

有关更详细、更最新的解释,请观看来自 JavaOne 2016 的 Project Jigsaw:Under the Hood 视频2

...为什么错误引用

sisu.inject.bean
aopalliance
模块...

因为你的项目有一个模块描述符 (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 项目拼图:幕后花絮—随附幻灯片
3 Rémi Forax — 分割包问题— Jigsaw 开发者邮件列表 — 2016 年 11 月。


0
投票

我找到了另一个解决方案。当您使用 module-info.java 文件和 Surefire 插件时,最好的解决方案是使用“useModulePath”选项

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
        <useModulePath>false</useModulePath>
    </configuration>
</plugin>

参见此处 让 Maven Surefire 在存在 module-info.java 文件的类路径上运行

© www.soinside.com 2019 - 2024. All rights reserved.