【发布时间】:2020-01-27 05:44:21
【问题描述】:
操作系统:Linux Mint 18.3,Eclipse“2019-06”。使用 Gradle 5.4 的 Gradle 包装器。
昨天我花了大约 4 个小时尝试在 Eclipse 中组合一个项目,该项目将在使用 Java 11 时实际运行并显示 JavaFX 场景。在here 的帮助下,我最终管理了它。
但这只是 Eclipse 中的一个沼泽标准“Java 项目”。我真正需要做的是开发一个 Gradle 项目,使用 Groovy 作为主要语言。
再次,我从同一位男士 José Pereda 那里找到了this answer。这可以通过下面的测试完成 Gradle“构建”。但是,当我使用 Gradle“应用程序”插件的任务“运行”时,我收到一条令人讨厌的消息:“错误:JavaFX 运行时组件丢失,并且需要运行此应用程序”。无论我是从 Eclipse 内部还是从终端运行,我都会得到这个。
这是我的 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}"
}
这是我的应用程序代码:
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“运行”后终端中的完整输出如下:
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 的指示进行操作。这就是我尝试执行“运行”任务的过程:
> 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 运行中得到了这个。
【问题讨论】:
-
JavaFX 不再与 Java 11 捆绑在一起。您必须单独包含它。
标签: java eclipse gradle javafx java-11