在模块路径上使用 OpenJFX 11 JMODS 在 JDK 11 上运行 javafx 示例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51843208/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 00:16:29  来源:igfitidea点击:

Running javafx sample on JDK 11 with OpenJFX 11 JMODS on Module Path

javajavafxjava-11openjfxjavafx-11

提问by MohamedSanaulla

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/jmodsi.e it doesn't come with JavaFX distribution.

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

Module info file:

模块信息文件:

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

    exports com.test;
}

I compile with following:

我编译如下:

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

But I get the below error:

但我收到以下错误:

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

采纳答案by José Pereda

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

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

This is explained here: http://openjdk.java.net/jeps/261#Packaging:-JMOD-files:

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

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.

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

and credit goes to this answer.

归功于这个答案

So I've done some simple module hellofx:

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

module hellofx {
    requires javafx.controls;

    exports hellofx;
}

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

HelloFX从样品这里并下载JavaFX的11 jmods我的平台,在这里。我还从同一位置下载了 JavaFX 11 SDK(jar)。

Compile time

编译时间

At compile time, we can do, with jmods:

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

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

or with SDK:

或使用 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.

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

Run time

运行

Now we want to run our little module.

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

With jmods, as stated by the OP, running:

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

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

fails with:

失败:

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

But using the SDK, works:

但是使用 SDK,可以工作:

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

Link time

链接时间

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

正如 JEP-261 所述,jmods 在链接时也能正常工作,因此我们可以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 工具将一组模块及其依赖项组装和优化为自定义运行时映像。(来源

So let's do:

所以让我们这样做:

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

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

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

Run time (2)

运行时间 (2)

And we can finally do:

我们终于可以做到:

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

And that will work.

这会奏效。

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.

总之,如果我们只想使用 jmods 来编译和运行我们的模块,我们需要使用jlink. 否则,对于运行时,我们将需要 JavaFX SDK。

回答by Searjasub

If it is not automatically added it, try using this setup in the pom.xml. Be sure to change the "!!YOUR MAIN CLASSNAME HERE!!" towards the bottom of the code to the name of your class with the main method! If my class is Example.java I will want to put in there just Example.

如果没有自动添加它,请尝试在 pom.xml 中使用此设置。一定要更改“!!YOUR MAIN CLASSNAME HERE!!” 使用 main 方法将代码的底部指向您的类的名称!如果我的类是 Example.java,我只想把 Example 放在那里。

 <dependencies>
    <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-controls</artifactId>
        <version>11.0.2</version>
    </dependency>
    <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-fxml</artifactId>
        <version>11.0.2</version>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.0</version>
            <configuration>
                <release>11</release>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.6.0</version>
            <executions>
                <execution>
                    <goals>
                        <goal>java</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <mainClass>!!YOUR MAIN CLASSNAME HERE!!</mainClass>
            </configuration>
        </plugin>
    </plugins>
</build>

JavaFX is not automatically added as a dependency with Java 11. That's why we need added manually.

JavaFX 不会作为 Java 11 的依赖项自动添加。这就是我们需要手动添加的原因。