在模块路径上使用 OpenJFX 11 JMODS 在 JDK 11 上运行 javafx 示例 [英] Running javafx sample on JDK 11 with OpenJFX 11 JMODS on Module Path

查看:45
本文介绍了在模块路径上使用 OpenJFX 11 JMODS 在 JDK 11 上运行 javafx 示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经从 OpenJFX 项目下载了 JavaFX Jmod 文件并将它们放在目录 G:openjfxjavafx-jmods-11 中.我正在使用 OpenJDK 11,它在 JAVA_HOME/jmods 中没有 JavaFX jmod,即它不附带 JavaFX 发行版.

I have downloaded the JavaFX Jmod files from OpenJFX project and placed them in the directory G:openjfxjavafx-jmods-11. I am using OpenJDK 11 which has no JavaFX jmod in JAVA_HOME/jmods i.e it doesn't come with JavaFX distribution.

模块信息文件:

module gui{
    requires javafx.graphics;
    requires javafx.controls;

    exports com.test;
}

我编译如下:

javac -p G:openjfxjavafx-jmods-11 -d mods --module-source-path src 
    srcguicom	est*.java srcguimodule-info.java

编译成功.但我无法使用以下命令运行编译后的代码:

Compilation succeeds. But I am unable to run the compiled code using the below command:

java -p G:openjfxjavafx-jmods-11;mods -m gui/com.test.CreateGuiDemo

但我收到以下错误:

Error occurred during initialization of boot layer
java.lang.module.FindException: Module javafx.graphics not found, required by gui

推荐答案

我相信对您所面临的错误有一个解释:jmods can't be used at run time.

I believe there is an explanation for the error you are facing: jmods can't be used at run time.

这里解释了:http://openjdk.java.net/jeps/261#Packaging:-JMOD-files:

JMOD 文件可以在编译时和链接时使用,但不能在运行时使用.要在运行时支持它们,通常需要我们准备好即时提取和链接本机代码库.

JMOD files can be used at compile time and link time, but not at run time. To support them at run time would require, in general, that we be prepared to extract and link native-code libraries on-the-fly.

归功于此答案.

所以我做了一些简单的模块hellofx:

So I've done some simple module hellofx:

module hellofx {
    requires javafx.controls;

    exports hellofx;
}

使用来自此处HelloFX示例并下载了来自此处的适用于我的平台的 JavaFX 11 的 jmods.我还从同一位置下载了 JavaFX 11 SDK(jar).

with the HelloFX sample from here and downloaded the jmods for JavaFX 11 for my platform from here. I've also downloaded the JavaFX 11 SDK (jars) from the same location.

编译时间

在编译时,我们可以使用 jmods:

At compile time, we can do, with jmods:

javac -p /path-to/javafx-jmods-11/ -d mods/hellofx $(find src/hellofx -name "*.java")

或使用 SDK:

javac -p /path-to/javafx-sdk-11/lib -d mods/hellofx $(find src/hellofx -name "*.java")    

在这两种情况下,结果完全相同,正如预期的那样:编译时不需要本机库.

In both cases, the result is exactly the same, as expected: Native libraries are not required during compile time.

运行时间

现在我们要运行我们的小模块.

Now we want to run our little module.

使用 jmods,如 OP 所述,运行:

With jmods, as stated by the OP, running:

java -p /path-to/javafx-jmods-11/:mods -m hellofx/hellofx.HelloFX   

失败:

Error occurred during initialization of boot layer
  java.lang.module.FindException: Module javafx.controls not found, required by hellofx

但是使用 SDK 可以:

But using the SDK, works:

java -p /path-to/javafx-sdk-11/lib/:mods -m hellofx/hellofx.HelloFX

链接时间

如 JEP-261 所述,jmods 在链接时也能正常工作,因此我们可以在编译时和运行时之间使用 jlink 工具.

As stated by the JEP-261, jmods work as well at link time, so we can use the jlink tool in between compile time and run time.

您可以使用 jlink 工具将一组模块及其依赖项组装和优化为自定义运行时映像.(来源)

You can use the jlink tool to assemble and optimize a set of modules and their dependencies into a custom runtime image. (source)

所以让我们这样做:

jlink -p /path-to/javafx-jmods-11/:mods --add-modules=hellofx --output links

这将生成一个 90.7 MB 的文件夹(在我的 Mac 上).请注意,lib 文件夹包含 Java 11 和 JavaFX 11 中所有必需的本机库,以及一个名为 modules 的 70.5 MB 文件.

that will generate a folder with 90.7 MB (on my Mac). Note that the lib folder contains all the required native libraries from Java 11 and from JavaFX 11, as well as a 70.5 MB file named modules.

运行时间 (2)

我们终于可以做到:

links/bin/java -m hellofx/hellofx.HelloFX

这会奏效.

总而言之,如果我们只想使用 jmods 来编译和运行我们的模块,我们需要使用 jlink 提供一个额外的步骤.否则,对于运行时,我们将需要 JavaFX SDK.

In conclusion, if we want to use only jmods for compiling and running our modules, we need to give an extra step with jlink. Otherwise, for runtime we'll need the JavaFX SDK.

这篇关于在模块路径上使用 OpenJFX 11 JMODS 在 JDK 11 上运行 javafx 示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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