导致此错误的原因是什么? “模块 main 包含包 javafx.fxml,模块 javafx.fxml 将包 javafx.fxml 导出到 main”

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

根据我的理解和阅读,问题是该模块在某个地方导入了两次,但我不明白在哪里。这是我的 module-info.java 文件:

module main {
    requires org.apache.logging.log4j;
    requires javafx.controls;
    requires javafx.fxml;
    requires java.sql;
}

这是我的build.gradle

plugins {
    id 'java'
    id 'application'
    id 'org.javamodularity.moduleplugin' version '1.8.12'
    id 'org.openjfx.javafxplugin' version '0.0.13'
    id 'org.beryx.jlink' version '2.25.0'
}

group 'org'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

ext {
    junitVersion = '5.10.0'
}

sourceCompatibility = '21'
targetCompatibility = '21'

tasks.withType(JavaCompile).configureEach {
    options.encoding = 'UTF-8'
}

application {
    mainModule = 'main'
    mainClass = 'main.HelloApplication'
}

javafx {
    version = '21'
    modules = ['javafx.controls', 'javafx.fxml']
}

dependencies {
    implementation('org.controlsfx:controlsfx:11.1.2')

    implementation('com.dlsc.formsfx:formsfx-core:11.6.0') {
        exclude(group: 'org.openjfx')
    }
    implementation('net.synedra:validatorfx:0.4.0') {
        exclude(group: 'org.openjfx')
    }
    implementation('org.kordamp.bootstrapfx:bootstrapfx-core:0.4.0')

    // https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core
    implementation 'org.apache.logging.log4j:log4j-core:2.22.1'

    // https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-api
    implementation 'org.apache.logging.log4j:log4j-api:2.22.0'

    testImplementation platform('org.junit:junit-bom:5.9.1')
    testImplementation 'org.junit.jupiter:junit-jupiter'


    // https://mvnrepository.com/artifact/com.mysql/mysql-connector-j
    runtimeOnly 'com.mysql:mysql-connector-j:8.3.0'
}

test {
    useJUnitPlatform()
}

jar {
    duplicatesStrategy = DuplicatesStrategy.EXCLUDE
    manifest {
        attributes('Main-Class': 'main.HelloApplication', 'Multi-Release': 'true')
    }
    from {
        configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
    }
}


jlink {
    imageZip = project.file("${buildDir}/distributions/app-${javafx.platform.classifier}.zip")
    options = ['--strip-debug', '--compress', '2', '--no-header-files', '--no-man-pages']
    launcher {
        name = 'app'
    }
}

jlinkZip {
    group = 'distribution'
}

我尝试更改我的模块信息文件一堆,如果这个错误得到解决,那么我每次都会有一个不同的文件,例如:

module main {
    requires org.apache.logging.log4j;
    requires javafx.controls;
    requires javafx.fxml;
    requires java.sql;
    requires org.controlsfx.controls;
    requires com.dlsc.formsfx;
    requires org.kordamp.bootstrapfx.core;

    exports main;
    exports main.model;
    exports main.repository;
    exports main.service;
    exports main.controller;

    opens main;
    opens main.model;
    opens main.repository;
    opens main.service;
    opens main.controller;
}

现在将生成“模块 main 包含包 javafx.beans.value,模块 javafx.base 将包 javafx.beans.value 导出到 main”或在另一个运行中“模块 main 包含包 javafx.event,模块 javafx.base 导出包 javafx” .事件到主要”。

java gradle package java-module
1个回答
0
投票

TL;DR: 开发模块化应用程序时不要创建胖 JAR 文件。


该错误意味着两个模块包含相同的包。 Java Platform Module System (JPMS) 不允许拆分包。在这种情况下,错误表明您自己的模块包含来自 JavaFX 模块的包。

您已配置

jar
任务来创建所谓的 fat JAR 文件(即具有依赖项的 JAR)。这些依赖项包括 JavaFX 模块。但这至少会导致
run
任务出现问题。当您的代码是模块化的时,
run
任务依赖于
jar
任务并将JAR文件作为模块执行1
run
任务还将依赖于模块路径2。这些依赖项包括 JavaFX 模块3。因此,您现在已将 JavaFX 包在您的模块和 JavaFX 模块之间分开,并且您会看到错误。

我怀疑执行

jlink
任务时会出现同样的问题,可能还有处理 JPMS 模块的所有任务,但我不太熟悉 The Badass JLink Plugin

解决方案是从

jar
任务配置中删除以下内容:

from {
    configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
}

顺便说一句,如果您更喜欢将应用程序部署为 fat JAR 文件(而不是通过 jlink/jpackage),那么我建议您将项目设为非模块化。您还应该从 fat JAR 文件中排除任何

module-info.class
文件。关键的是,你的主类一定不能是
Application
的子类;您必须创建一个单独的“启动器”主类,该主类至少可以简单地启动 JavaFX 应用程序。


1。当您的代码是非模块化时,

run
任务仅依赖于
compileJava
processResources
任务(间接通过
classes
任务)。模块化和非模块化项目之间的行为差异是解决“有关资源和模块的问题”的一部分。

2。

org.openjfx.javafxplugin 插件将修改

run
任务,将 JavaFX 放在
非模块化
项目的模块路径上。换句话说,无论如何,JavaFX 将最终位于模块路径上。

3.依赖项不仅包括 JavaFX,还包括您声明的所有其他非测试依赖项。您收到专门与 JavaFX 包相关的错误,但所有依赖项都存在该问题。

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