问题
I'm trying to run a sample JavaFX app on Mac OS.
build.gradle
apply plugin: 'java'
apply plugin: 'application'
repositories {
mavenCentral()
}
dependencies {
compile "org.openjfx:javafx-base:11"
compile "org.openjfx:javafx-graphics:11"
compile "org.openjfx:javafx-controls:11"
}
compileJava {
doFirst {
println "CLASSPATH IS $classpath.asPath"
options.compilerArgs = [
'--module-path', classpath.asPath,
'--add-modules', 'javafx.graphics'
]
classpath = files()
}
}
Java class
package com.test;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.stage.Stage;
public class HelloFX extends Application {
@Override
public void start(Stage stage) {
String javaVersion = System.getProperty("java.version");
String javafxVersion = System.getProperty("javafx.version");
Label l = new Label("Hello, JavaFX " + javafxVersion + ", running on Java " + javaVersion + ".");
Scene scene = new Scene(l, 640, 480);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch();
}
}
I use Gradle 4.10.2
Executing task 'gradle compileJava' I'm getting this error:
> Task :compileJava FAILED
CLASSPATH IS /Users/dragos.pruteanu/.gradle/caches/modules-2/files-2.1/org.openjfx/javafx-controls/11/58d961774262ec972bf304e16c154a8e18c2050b/javafx-controls-11.jar:/Users/dragos.pruteanu/.gradle/caches/modules-2/files-2.1/org.openjfx/javafx-graphics/11/a736dd079047ec0b72b8c4970842a5c5e0c19f2f/javafx-graphics-11.jar:/Users/dragos.pruteanu/.gradle/caches/modules-2/files-2.1/org.openjfx/javafx-base/11/9fcd3e8e3227ec97bf503f7991fad1f3b14d005/javafx-base-11.jar
error: module not found: javafx.graphics
1 error
What is wrong? For some reason the JavaFX libraries are not loaded correctly. Could be the error from MacOS or OpenJFX?
回答1:
The reason why it would fail for you is mostly that the automatic module name derived out of the jar that you've used would not be javafx.graphics
. Trying to get the details using the command line, I could observe the following:
jar --file=.../org/openjfx/javafx-graphics/11/javafx-graphics-11.jar --describe-module No module descriptor found. Derived automatic module. javafx.graphicsEmpty@11 automatic requires java.base mandated
and since the module name resolved is not same as what you've specified in the command line --add-modules javafx.graphics
, hence you're facing the stated error.
Additionally, one of the notes from Run HelloWorld using JavaFX 11 reads:
there is no need to add
javafx.graphics
module, since it is transitively required by thejavafx.controls
module
Edit from comments:- Steps defined at Run HelloWorld using Gradle with JavaFX would be a better place to look for appropriate steps to build with gradle.
As it states(edits mine), one needs to specify the platform in dependencies for e.g.
compile "org.openjfx:javafx-graphics:11:$platform"
... classifiers are not taken into account when resolving transitive dependencies in Gradle. Therefore, we need to specify ... modules with platform as classifier
and for which you might need the build script used in the sample as well to specify the platform/OS as a classifier.
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.google.gradle:osdetector-gradle-plugin:1.6.0'
}
}
apply plugin: 'application'
apply plugin: 'com.google.osdetector'
ext.platform = osdetector.os == 'osx' ? 'mac' : osdetector.os == 'windows' ? 'win' : osdetector.os
来源:https://stackoverflow.com/questions/53148771/gradle-for-java-11-with-modules