有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Java11、JavaFX、Gradle、Eclipse、严重故障消息

操作系统:Linux Mint 18.3,Eclipse“2019-06”。使用Gradle 5.4的Gradle包装器

昨天我花了大约4个小时试图在Eclipse中构建一个项目,该项目在使用Java11时实际运行并显示JavaFX场景。由于得到了here的帮助,我最终成功了

但这只是Eclipse中的一个bog标准“Java项目”。我实际上需要做的是开发一个Gradle项目,使用Groovy作为主要语言

再一次,我从同一位先生JoséPereda那里找到了this answer。在下面的测试中,这是一个渐变的“构建”OK。但是,当我使用Gradle“application”插件的任务“run”时,我收到了一条令人不快的消息:“错误:缺少JavaFX运行时组件,运行此应用程序需要这些组件”。无论是从Eclipse内部运行还是从终端运行,我都会得到这个结果

这就是我的身材。格拉德尔:

plugins {
     id 'java-library'
     id 'groovy'
     id 'eclipse'
     id 'application'
     id 'java'
}
mainClassName = 'core.App'
group 'Project'
version '1.0'
sourceCompatibility = 1.11
repositories {
     mavenCentral()
}
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'
}
dependencies {
     api 'org.apache.commons:commons-math3:3.6.1'
     implementation 'com.google.guava:guava:27.0.1-jre'
     testImplementation 'junit:junit:4.12'
     implementation 'org.codehaus.groovy:groovy-all:2.5.8'
     testImplementation 'org.spockframework:spock-core:1.2-groovy-2.5'
     // these seemed to do the trick for a simple "Java project"
     // ... but Gradle seems to want something else (?)
     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}"
}

这是我的申请代码:

import javafx.application.Application
import javafx.stage.Stage

class App extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception {
        println "start...."
    }

    public static void main(String[] args) {
        println "about to launch..."
        launch(args);
        println "...launched"
    }
}

这是我的测试:

class FuncSpec extends Specification {
    def "JavaFX should run OK"(){
        when:
        App app = new App()

        then:
        true
    }
}

渐变“运行”后终端的全输出如下:

mike@M17A ~/software projects/eclipse-workspace/GrVocabSearch2019-09 $  ./gradlew run

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

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':run'.
> Process 'command '/usr/lib/jvm/java-11-openjdk-amd64/bin/java'' finished with non-zero exit value 1

后来

按照何塞·佩雷达(JoséPereda)的指示,在我的身材中做到了这一点。格拉德尔。这就是我尝试执行“运行”任务时得到的结果:

> Task :run FAILED
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.codehaus.groovy.vmplugin.v7.Java7$1 (file:/media/chris/W10%20D%20drive/apps/Chris.gradle/caches/modules-2/files-2.1/org.codehaus.groovy/groovy/2.5.8/2f1e8ea55e625fe51e85ef35eb067f1d9c61772d/groovy-2.5.8.jar) to constructor java.lang.invoke.MethodHandles$Lookup(java.lang.Class,int)
WARNING: Please consider reporting this to the maintainers of org.codehaus.groovy.vmplugin.v7.Java7$1
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release

java.lang.reflect.InvocationTargetException
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:566)
        at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
        at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:566)
        at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051)
Caused by: java.lang.RuntimeException: Error: class jdk.internal.reflect.NativeMethodAccessorImpl is not a subclass of javafx.application.Application
        at javafx.graphics/javafx.application.Application.launch(Application.java:298)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:566)
        at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:101)
        at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:323)
        at groovy.lang.MetaClassImpl.invokeStaticMethod(MetaClassImpl.java:1491)
        at org.codehaus.groovy.runtime.callsite.StaticMetaClassSite.callStatic(StaticMetaClassSite.java:62)
        at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallStatic(CallSiteArray.java:55)
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callStatic(AbstractCallSite.java:196)
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callStatic(AbstractCallSite.java:208)
        at core.App.main(main.groovy:60)
        ... 11 more
Exception running application core.App

FAILURE: Build failed with an exception

我相信这不是一个没有提到格拉德的答案的复制品。我已经在正常的Java运行中实现了这一点


共 (1) 个答案

  1. # 1 楼答案

    问题的原因是:

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

    已经被回答了很多次(包括Gradle),但是由于您仍然使用“旧”方法来包含JavaFX依赖项,而不是使用JavaFX插件,我将解释如何解决这个问题

    错误

    如果您查看JavaFX文档中的Eclipse部分IDE(模块化或非模块化项目),就会清楚地解释这个问题:

    Error JavaFX

    或者,换句话说,JavaFX依赖项是模块化的,必须添加到模块路径中

    第一个解决方案

    如果您继续阅读,您将立即找到关于如何修复它的解释(前提是您没有使用构建工具):

    Solution JavaFX

    但是,由于您使用的是Gradle,因此必须对解决方案进行调整,以将其包含在构建文件中

    这就是使用JavaFX插件之前的方法:

    compileJava {
        doFirst {
            options.compilerArgs = [
                    ' module-path', classpath.asPath,
                    ' add-modules', 'javafx.controls,javafx.fxml'
            ]
        }
    }
    
    run {
        doFirst {
            jvmArgs = [
                    ' module-path', classpath.asPath,
                    ' add-modules', 'javafx.controls,javafx.fxml'
            ]
        }
    }
    

    现在,您应该能够运行:

    ./gradlew run
    

    顺便说一下,在使用插件之前,您仍然可以看到here文档中引用的HelloFX示例的构建

    JavaFX插件

    Gradle的JavaFX plugin的创建正是为了处理构建文件中的所有“样板代码”

    如果您现在查看Eclipse、Gradle(模块化或非模块化)或相同更新的sample的文档,您将看到构建文件简化为:

    plugins {
      id 'application'
      id 'org.openjfx.javafxplugin' version '0.0.8'
    }
    
    repositories {
        mavenCentral()
    }
    
    dependencies {
    }
    
    javafx {
        version = "13"
        modules = [ 'javafx.controls', 'javafx.fxml' ]
    }
    
    mainClassName = 'org.openjfx.MainApp'
    

    在您的情况下,您只需要将其适应于构建文件,例如:

    plugins {
        id 'java-library'
        id 'groovy'
        id 'eclipse'
        id 'application'
        id 'org.openjfx.javafxplugin' version '0.0.8'
    }
    
    mainClassName = 'core.App'
    group 'Project'
    version '1.0'
    sourceCompatibility = 11
    
    repositories {
        mavenCentral()
    }
    
    dependencies {
        api 'org.apache.commons:commons-math3:3.6.1'
        implementation 'com.google.guava:guava:27.0.1-jre'
        testImplementation 'junit:junit:4.12'
        implementation 'org.codehaus.groovy:groovy-all:2.5.8'
        testImplementation 'org.spockframework:spock-core:1.2-groovy-2.5'
    }
    
    javafx {
        version = "13"
        modules = [ "javafx.controls", "javafx.fxml" ]
    }
    

    并运行:

    ./gradlew run