Gradle for Java 11 with Modules [英] Gradle for Java 11 with Modules

查看:169
本文介绍了Gradle for Java 11 with Modules的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Mac OS上运行示例JavaFX应用。

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类

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();
    }

}

我使用Gradle 4.10.2

I use Gradle 4.10.2

执行任务'gradle compileJava'我收到此错误:

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

有什么问题?由于某种原因,未正确加载JavaFX库。可能是来自MacOS或OpenJFX的错误?

What is wrong? For some reason the JavaFX libraries are not loaded correctly. Could be the error from MacOS or OpenJFX?

推荐答案

它失败的原因主要是自动从您使用的jar中派生的模块名称 不是 javafx.graphics 。尝试使用命令行获取详细信息,我可以观察到以下内容:

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


并且因为已解析的模块名称与您在命令行中指定的名称不同 - add-modules javafx.graphics 因此,您将面临规定的错误。

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:


没有必要添加 javafx.graphics 模块,因为<$ c需要
传递 $ c> javafx.controls 模块

there is no need to add javafx.graphics module, since it is transitively required by the javafx.controls module






从评论中编辑 : - 使用HelloWorld运行HelloWorld使用JavaFX的Gradle 将是一个更好的地方,可以寻找适当的步骤来构建机智h gradle。


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"




...在Gradle中解析传递
依赖项时不考虑分类器。因此,我们需要指定...
模块,其中平台为分类器

... classifiers are not taken into account when resolving transitive dependencies in Gradle. Therefore, we need to specify ... modules with platform as classifier

并且您可能需要构建脚本在示例中也用于将平台/ OS指定为分类器。

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

这篇关于Gradle for Java 11 with Modules的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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