错误:缺少 JavaFX 运行时组件,并且需要使用 Gradle 示例来运行此应用程序

Eos*_*gen 3 javafx gradle

我知道这个问题已被问过多次......但我似乎找不到解决方案。

取自这个官方指南示例: https: //openjfx.io/openjfx-docs/#gradle 我继续并添加到我的build.gradle中:

plugins {
    id 'application'
    id 'org.openjfx.javafxplugin' version '0.0.8'
}

javafx {
    version = '13'
    modules = ['javafx.controls']
}

repositories {
    mavenCentral()
}

mainClassName = "MyImage"

jar {
    manifest {
        attributes "Main-Class": "$mainClassName"
    }
    from {
        configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
    }
}
Run Code Online (Sandbox Code Playgroud)

通过运行gradle jar(或gradle build),实际上应该生成一个 jar,其中应包含构建它所用的所有包,即整个 javafx 库。

但是,当它成功构建后,我继续运行:

java -jar build/libs/MyImage.jar

它仍然抛出错误:

Error: JavaFX runtime components are missing, and are required to run this application

我缺少什么?

(我使用JDK 11)

非常感谢。

Ada*_*dam 6

在 Java 11 中,Java 启动器检测到您正在扩展 javafx.application.Application 并检查模块是否存在。如果您使用的是普通的旧 JAR,那么您会收到错误消息

Error: JavaFX runtime components are missing, and are required to run this application
Run Code Online (Sandbox Code Playgroud)

你有两个选择。设置您的应用程序以使用 Java 模块系统或以下解决方法。

此解决方法避免了 Java 启动器检查并让应用程序运行。

public class MyImage {   // <=== note - does not extend Application

    public static class YourRealApplication extends Application {

        @Override
        public void start(Stage primaryStage) throws Exception {
            // whatever...
        }

    }

    public static void main(String[] args) {
        Application.launch(YourRealApplication.class);
    }

}
Run Code Online (Sandbox Code Playgroud)