运行 JDK 11 上的 javafx 示例,模块路径上有 OpenJFX 11 JMODS

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

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

模块信息文件:

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

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

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 can't be used at 运行 time.

这里有解释: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.

归功于此 answer

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

module hellofx {
    requires javafx.controls;

    exports hellofx;
}

使用 here and downloaded the jmods for JavaFX 11 for my platform from here 中的 HelloFX 个示例。我还从同一位置下载了 JavaFX 11 SDK(罐子)。

编译时间

在编译时,我们可以用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")    

在这两种情况下,结果与预期完全相同:编译时不需要本机库。

运行时间

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

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

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,有效:

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

Link时间

正如 JEP-261 所述,jmods 在 link 时间也能正常工作,因此我们可以在编译时间和 运行 时间之间使用 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.[=29= 的 70.5 MB 文件]

运行次(2)

我们终于可以做到:

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

这会奏效。

总而言之,如果我们只想使用 jmods 来编译和 运行ning 我们的模块,我们需要使用 jlink 进行额外的步骤。否则,运行时间我们将需要 JavaFX SDK。

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

 <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 不会自动添加为 Java11 的依赖项。这就是我们需要手动添加的原因。