【问题标题】:Error: JavaFX runtime components are missing, and are required to run this application with Gradle example错误:缺少 JavaFX 运行时组件,需要使用 Gradle 示例运行此应用程序
【发布时间】:2020-01-16 14:03:03
【问题描述】:

我知道这已被多次询问...但我似乎无法找到解决方案。

取自官方指南示例: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) }
    }
}

通过运行 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)

非常感谢。

【问题讨论】:

    标签: gradle javafx


    【解决方案1】:

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

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

    你有两个选择。设置您的应用程序以使用 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);
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2013-01-17
      • 2022-01-17
      • 2019-02-07
      • 2020-04-20
      • 2019-03-25
      • 2018-12-30
      • 1970-01-01
      相关资源
      最近更新 更多