使用Maven和jlink的OpenJFX自定义运行时映像-模块导出还是命令行参数? [英] OpenJFX Custom runtime image using Maven and jlink - Module exports or command line arguments?
问题描述
我正在尝试创建一个自定义运行时映像,该映像不需要在计算机上安装JRE/JDK.我已经按照OpenJFX文档上介绍的教程(JavaFX和IntelliJ-使用Maven进行模块化)进行操作,并且能够运行创建的图像,但是我想为我的应用程序类com.sun.glass.ui.Window包含(在javafx模块中) .graphics).
I'm trying to create a custom runtime image that will not require installed JRE/JDK on the computer. I have followed the tutorial presented on OpenJFX Documentation (JavaFX and IntelliJ - Modular with Maven) and I am able to run the created image, but I want to include for my application class com.sun.glass.ui.Window (in module javafx.graphics).
在自定义图像之前,我将以下内容解析为命令行参数: --add-打开javafx.graphics/com.sun.glass.ui=全部取消命名
Before custom images, I was parsing as command line arguments the following: --add-opens javafx.graphics/com.sun.glass.ui=ALL-UNNAMED
我想在运行时中包含它,所以我应该修改Maven的pom以包括javafx-maven-plugin一个(不成功),还是应该编辑项目module.info以从javafx.graphics导出请求的包
I want to include this in runtime, so should I modify the pom from Maven to include for javafx-maven-plugin a (not successful) or should I edit the project module.info to export the requested package from javafx.graphics.
谢谢, 安德烈(Andrei)
Thanks, Andrei
Pom.xml module.info.java
Pom.xml module.info.java
module com.andrei {
requires javafx.controls;
requires javafx.graphics;
exports com.andrei;
exports com.sun.glass.ui to com.andrei;
}
在模块"javafx.graphics"中声明软件包"com.sun.glass.ui",该软件包不会导出到模块"com.andrei"".
"Package "com.sun.glass.ui" is declared in module "javafx.graphics", which does not export to module "com.andrei" "
推荐答案
javafx-maven-plugin
应该能够完成您想做的事情.但是,到目前为止,它还没有做到这一点,所以我只提交了以下两个问题:缺少链接虚拟机选项参数.
The javafx-maven-plugin
should be able to do what you are trying to do. However, it doesn't do it so far, so I've just filed these two issues: Options for javafx:run are incompatible with javafx:jlink and Missing link vm options parameter.
此问题得到解决并发布了新版本时,有一个简单的(但手动的)修复程序:
While this gets resolved and a new version is published, there is an easy (but manual) fix:
编译时间
在修改javafx-maven-plugin
之前,您需要允许您的IDE与私有软件包一起使用.您不能从module-info中做到这一点,但是您可以使用compilerArgs
从maven-compiler-plugin
中轻松做到这一点:
Before modifying the javafx-maven-plugin
, you need to allow your IDE to work with the private package. You can't do it from the module-info, but you can easily do that from the maven-compiler-plugin
using compilerArgs
:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<compilerArgs>
<arg>--add-exports</arg>
<arg>javafx.graphics/com.sun.glass.ui=com.andrei</arg>
</compilerArgs>
</configuration>
</plugin>
现在,在您的代码中,您可以使用该私有包,并且IntelliJ不会抱怨.
Now in your code you can make use of that private package, and IntelliJ won't complain.
在Maven窗口Lifecycle -> clean
和Lifecycle -> compile
中运行后,在编辑器中允许这样的操作:
After running from the Maven window Lifecycle -> clean
, and Lifecycle -> compile
, something like this is allowed in the editor:
@Override
public void start(Stage stage) throws Exception {
...
stage.setScene(scene);
stage.show();
com.sun.glass.ui.Window.getWindows().forEach(System.out::println);
}
运行时
但是,如果您执行mvn clean compile javafx:run
,则上面的代码将失败:
However, if you do mvn clean compile javafx:run
, the code above will fail:
原因:java.lang.IllegalAccessError:com.andrei.Main类(在com.andrei模块中)无法访问com.sun.glass.ui.Window类(在javafx.graphics模块中),因为模块javafx.graphics确实可以不会将com.sun.glass.ui导出到模块com.andrei.
Caused by: java.lang.IllegalAccessError: class com.andrei.Main (in module com.andrei) cannot access class com.sun.glass.ui.Window (in module javafx.graphics) because module javafx.graphics does not export com.sun.glass.ui to module com.andrei.
如插件自述文件所述,您cad添加了VM将传递给java
工具的选项:
As explained in the plugin readme, you cad add VM options that will be passed to the java
tool:
<plugin>
<groupId>org.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>0.0.2</version>
<configuration>
<options>
<option>--add-opens</option>
<option>javafx.graphics/com.sun.glass.ui=com.andrei</option>
</options>
...
</configuration>
</plugin>
现在您可以运行:mvn clean compile javafx:run
,这将起作用,并且您将获得打印出当前阶段的信息.
Now you can run: mvn clean compile javafx:run
, and that will work, and you will get information for the current stage printed out.
运行时图像
最后,如果您运行:mvn clean compile javafx:jlink
,这将失败,因为jlink
不能识别<options>
中的内容(已提交第一期),因此您必须将其注释掉:
Finally, if you run: mvn clean compile javafx:jlink
, this will fail, because the content in <options>
is not recognized by jlink
(first issue filed), so you have to comment it out:
<plugin>
<groupId>org.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>0.0.2</version>
<configuration>
<!--<options>-->
<!--<option>--add-opens</option>-->
<!--<option>javafx.graphics/com.sun.glass.ui=com.andrei</option>-->
<!--</options>-->
<launcher>launcher</launcher>
<mainClass>com.andrei/com.andrei.Main</mainClass>
...
</configuration>
</plugin>
现在mvn clean compile javafx:jlink
可以使用,但是在运行时,由于未导出私有软件包,您将得到与上述相同的错误.
Now mvn clean compile javafx:jlink
will work, but when running you will get the same error as above because the private package is not exported.
但是,您可以在target/image/bin/launcher
下编辑启动器文件:
However, you can edit the launcher file under target/image/bin/launcher
:
#!/bin/sh
JLINK_VM_OPTIONS=
DIR=`dirname $0`
$DIR/java $JLINK_VM_OPTIONS -m com.andrei/com.andrei.Main $@
如您所见,有一个空的JLINK_VM_OPTIONS
变量可以用您的vm选项填充.
As you can see, there is an empty JLINK_VM_OPTIONS
variable that can be filled with your vm options.
在解决第二个问题之前,只需修改该行:
Until the second issue filed is solved, just modify that line:
#!/bin/sh
JLINK_VM_OPTIONS="--add-opens javafx.graphics/com.sun.glass.ui=com.andrei"
DIR=`dirname $0`
$DIR/java $JLINK_VM_OPTIONS -m fx/org.openjfx.MainApp $@
保存并运行:target/image/bin/launcher
,它将起作用.
save, and run: target/image/bin/launcher
, and it will work.
这篇关于使用Maven和jlink的OpenJFX自定义运行时映像-模块导出还是命令行参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!