Java 11,JavaFX,Gradle,Eclipse,讨厌的失败消息 [英] Java 11, JavaFX, Gradle, Eclipse, nasty failure message

查看:379
本文介绍了Java 11,JavaFX,Gradle,Eclipse,讨厌的失败消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

OS:Linux Mint 18.3,Eclipse"2019-06".使用Gradle 5.4的Gradle包装器.

OS: Linux Mint 18.3, Eclipse "2019-06". Gradle wrapper using Gradle 5.4.

昨天,我花了大约4个小时试图在Eclipse中整理一个项目,该项目在使用Java 11时将实际运行并显示JavaFX Scene.由于得到了

Yesterday I spent about 4 hours trying to put together a project in Eclipse which would actually run and display a JavaFX Scene when using Java 11. I managed it in the end due to help from here.

但这只是Eclipse中一个沼泽标准的"Java项目".我实际上需要做的是使用Groovy作为主要语言来开发Gradle项目.

But this was just a bog-standard "Java project" in Eclipse. What I actually need to do is develop a Gradle project, using Groovy as the main language.

同样,我从同一位绅士何塞·佩雷达(JoséPereda)找到这个答案.这可以通过下面的测试来完成Gradle的构建".但是,当我使用Gradle应用程序"插件的任务运行"时,我得到了一个讨厌的消息:错误:缺少JavaFX运行时组件,并且是运行此应用程序所必需的".无论是从Eclipse内部还是从终端运行,都可以得到它.

Again, from the same gentleman, José Pereda, I found this answer. This does a Gradle "build" OK with the test below. But I get a nasty message when I use the Gradle "application" plugin's task "run": "Error: JavaFX runtime components are missing, and are required to run this application". I get this whether I run from inside Eclipse or from a Terminal.

这是我的build.gradle:

So this is my build.gradle:

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}"
}

这是我的应用程序代码:

This is my application code:

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
    }
}

Gradle运行"后,在终端中的完整输出是这样的:

Full output in Terminal after Gradle "run" is like so:

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

以后

在我的build.gradle中遵循了JoséPereda的指示.这就是我尝试执行运行"任务的过程:

Later

Followed José Pereda's indications to the letter in my build.gradle. This is what I get on trying to do a "run" task:

> 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

注意

我相信这不是任何未提及Gradle的答案的重复.我已经在正常的Java运行中正常运行了.

Note

I believe this is not a duplicate of any answer which does not mention Gradle. I have got this working in a normal Java run.

推荐答案

问题的原因:

错误:缺少JavaFX运行时组件,并且是运行此应用程序所必需的

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

已经被回答过很多次了(包括Gradle),但是由于您仍在使用旧的"方法来包含JavaFX依赖项,而不是使用JavaFX插件,因此,我将说明如何解决此问题.

has been answered a number of times already (including with Gradle), but since you are still using the "old" approach to include the JavaFX dependencies, instead of using the JavaFX plugin, I'll explain how to fix this.

错误

如果您查看JavaFX文档中的 Eclipse ,IDE部分(可以是模块化的或非模块化项目),对此问题有明确的解释:

If you check the JavaFX documentation for Eclipse, section IDE (either modular or non-modular projects), there is a clear explanation of this issue:

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

Or, in other words, JavaFX dependencies are modular and have to be added to the module-path.

第一个解决方案

如果您继续阅读,将会发现有关如何解决它的说明(前提是您不使用构建工具):

If you keep reading, you will find immediately an explanation on how to fix it (providing you are not using build tools):

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

However, since you are using Gradle, the solution has to be adapted to be included in your build file.

这是使用JavaFX插件之前完成的方式:

This is the way it was done, before using the JavaFX plugin:

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

顺便说一句,您仍然可以看到

By the way, you can still see here the build of a HelloFX sample referred from the documentation, before using the plugin.

JavaFX插件

为Gradle创建的 JavaFX插件正是为处理所有样板"而创建的代码".

The JavaFX plugin for Gradle was created precisely to deal with all the "boilerplate code" in the build file.

如果现在检查Eclipse的文档,Gradle(模块化或非模块化)部分或相同的更新的

If you check now the documentation for Eclipse, sections Gradle (modular or non modular), or the same updated sample you will see that the build file is simplified to this:

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'

在您的情况下,您只需要将其调整为适合您的构建文件,例如:

In your case, you just need to adapt this to your build file, something like:

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

这篇关于Java 11,JavaFX,Gradle,Eclipse,讨厌的失败消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆