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

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

问题描述

我已经从OpenJFX项目下载了JavaFX Jmod文件,并将它们放在 G:\openjfx \ javafx-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:\openjfx\javafx-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:\openjfx\javafx-jmods-11 -d mods --module-source-path src 
    src\gui\com\test\*.java src\gui\module-info.java

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

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

java -p G:\openjfx\javafx-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无法在运行时使用

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 SDK(jars)。

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.

使用OP规定的jmods,运行:

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工具将一组模块及其依赖项组装和优化到自定义运行时i中法师。 (来源

让我们这样做:

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

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

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

这将有效。

总之,如果我们只想使用jmod进行编译并运行我们的模块,我们需要使用 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天全站免登陆