【问题标题】:Unsupported JavaFX configuration: classes were loaded from 'unnamed module @...'不受支持的 JavaFX 配置:类是从“未命名模块 @...”加载的
【发布时间】:2021-08-04 11:57:53
【问题描述】:

This question 看起来非常相似,如果不完全相同的话。在那个问题中,Slaw 的第二个建议涉及修复module-info 以进行正确的模块管理似乎是合适的。但这究竟是什么意思?或者更确切地说,javafx 抱怨的我的模块有问题还是它正在谈论的其他模块?

这是我的模块信息.java

module myprogram.main {
    requires javafx.controls;
    requires javafx.fxml;
    requires janino;
    requires commons.compiler;
    requires org.fxmisc.richtext;
    requires wellbehavedfx;
    requires zt.process.killer;
    requires zt.exec;
    exports com.bla.myprogram;
    opens com.bla.myprogram to javafx.fxml;
}

jdk:jdk16.0.1+9

java -jar .\MyProgram.jar

@Override
    public void start(Stage primaryStage) throws Exception {
        System.out.println("enter start");
        ...

 public static void main(String[] args) {
        System.out.println("enter main");
        launch();
    }
enter main
Aug 04, 2021 1:22:52 PM com.sun.javafx.application.PlatformImpl startup
WARNING: Unsupported JavaFX configuration: classes were loaded from 'unnamed module @6203f37a'
enter start

(如果有任何意义,这是我的 gradle.build)

plugins {
    id 'java-library'
    id 'application'
    id 'org.openjfx.javafxplugin' version "0.0.10"
}

group 'com.bla.myprogram'
version '0.3'

def currentOS = org.gradle.internal.os.OperatingSystem.current()
def platform
if (currentOS.isWindows()) {
    platform = 'win'
} else if (currentOS.isLinux()) {
    platform = 'linux'
} else if (currentOS.isMacOsX()) {
    platform = 'mac'
}

repositories {
    mavenCentral()
}

dependencies {
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.0'
    implementation group: 'org.slf4j', name: 'slf4j-api', version: '1.7.25'
    implementation group: 'org.slf4j', name: 'slf4j-simple', version: '1.7.25'
    implementation "org.openjfx:javafx-base:11:${platform}"
    implementation "org.openjfx:javafx-graphics:11:${platform}"
    implementation "org.openjfx:javafx-controls:11:${platform}"
    implementation "org.openjfx:javafx-fxml:11:${platform}"
    implementation group: 'org.codehaus.janino', name: 'janino', version: '3.0.7'
    implementation group: 'org.fxmisc.richtext', name: 'richtextfx', version: '0.10.6'
    implementation group: 'org.fxmisc.wellbehaved', name: 'wellbehavedfx', version: '0.3.3'
    implementation group: 'org.zeroturnaround', name: 'zt-process-killer', version: '1.10'
    implementation group: 'org.zeroturnaround', name: 'zt-exec', version: '1.12'
}

test {
    useJUnitPlatform()
}

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

mainClassName = 'com.bla.myprogram.Main'

jar {
    manifest {
        attributes "Main-Class": 'com.bla.myprogram.Main'
    }

    from { (configurations.runtimeClasspath).collect { it.isDirectory() ? it : zipTree(it) } } {
        exclude 'META-INF/*.RSA', 'META-INF/*.SF', 'META-INF/*.DSA'
        duplicatesStrategy = DuplicatesStrategy.EXCLUDE
    }
}

task createProperties(dependsOn: processResources) {
    doLast {
        new File("$buildDir/resources/main/version.properties").withWriter { w ->
            Properties p = new Properties()
            p['version'] = project.version.toString()
            p['name'] = rootProject.name.toString()
            p.store w, null
        }
    }
}

classes {
    dependsOn createProperties
}

【问题讨论】:

    标签: java gradle javafx java-module


    【解决方案1】:

    我认为这可能是答案:

    无法将多个模块放入同一个 jar 中,因为一个 jar 只能包含 1 个模块。所以,我想 shade 插件通过删除它正在使用的依赖项的模块信息文件来解决这个问题,这意味着 JavaFX 代码不会作为模块加载,并且你会收到这样的警告。 我认为摆脱警告的唯一方法是不使用阴影,而是将 JavaFX 模块保存为单独的 jar 文件,然后在运行应用程序时将其放在模块路径中。

    因此,显而易见的选择就是忽略警告。 Javafx 警告似乎很少表明任何有用的东西。但是,如果您将应用程序分发给其他用户,则并不总是将目光移开。

    另一个(天真的)选项是在启动时重定向错误流。这有点天真,因为可能遗漏了一些其他错误..

    public void start(Stage primaryStage) throws Exception {
            System.setErr(oldErr); // reference to default System.err
            ...
    public static void main(String[] args) {
            System.setErr(new PrintStream(new NullOutputStream())); 
            launch();
    }
    

    另一种选择是使用 jpackage 之类的东西而不是制作 jar。无论如何,这可能不是一个坏主意,因为每个操作系统的 javafx 库都不同。

    【讨论】:

    • 更不用说 shaded/fat/uber JAR 文件通常作为可执行 JAR 启动(例如,通过双击或通过 java -jar <jar-file>)。不幸的是,据我所知,可执行 JAR 不支持模块路径,至少不支持开箱即用。换句话说,包含在所述 JAR 文件中的所有内容都放置在类路径中,因此放置在 未命名模块 中。因此,即使存在模块描述符,它也会被忽略。
    猜你喜欢
    • 2021-08-17
    • 2021-08-23
    • 2021-07-09
    • 2020-04-24
    • 1970-01-01
    • 2014-06-24
    • 2017-02-08
    • 2020-01-05
    相关资源
    最近更新 更多