如何在Maven中使用GraalVM与JavaFX来编译本机映像?

huangapple go评论168阅读模式
英文:

How to use GraalVM with JavaFX to compile a native image in Maven?

问题

我有一个JavaFX项目,想要使用GraalVM Java虚拟机和相关的Native-Image工具将其编译成Linux二进制文件。我正在使用GraalVM Java 11版本20.1.0,以及通过Maven添加的Native Image Maven插件来实现这一目标。

以下是Maven插件的配置代码:

  1. <plugin>
  2. <groupId>com.oracle.substratevm</groupId>
  3. <artifactId>native-image-maven-plugin</artifactId>
  4. <version>19.2.1</version>
  5. <configuration>
  6. <mainClass>sample.NewMain</mainClass>
  7. <imageName>sample</imageName>
  8. <buildArgs>
  9. -H:ReflectionConfigurationFiles=/home/user/Documents/Projects/TestProject/src/main/java/sample/reflect-config.json -H:+ReportExceptionStackTraces
  10. </buildArgs>
  11. </configuration>
  12. <executions>
  13. <execution>
  14. <goals>
  15. <goal>native-image</goal>
  16. </goals>
  17. <phase>package</phase>
  18. </execution>
  19. </executions>
  20. </plugin>

最初,我遇到了一个错误,错误信息为“Warning: Aborting stand-alone image build due to reflection use without configuration.” 我使用了Native Image追踪代理来生成反射的配置文件,并将其传递给编译器插件,如下所示:
-H:ReflectionConfigurationFiles=/home/user/Documents/Projects/TestProject/src/main/java/sample/reflect-config.json -H:+ReportExceptionStackTraces

我还开启了堆栈跟踪异常报告功能。

然而,当我尝试编译成本地镜像时,我遇到了以下与本地库使用有关的错误:

  1. Warning: System method java.lang.System.loadLibrary invoked at com.sun.glass.utils.NativeLibLoader.loadLibraryInternal(NativeLibLoader.java:163)
  2. Warning: System method java.lang.System.loadLibrary invoked at com.sun.glass.utils.NativeLibLoader.loadLibraryInternal(NativeLibLoader.java:177)
  3. Warning: Aborting stand-alone image build due to loading native libraries without configuration.
  4. com.oracle.svm.hosted.FallbackFeature$FallbackImageRequest: System method java.lang.System.loadLibrary invoked at com.sun.glass.utils.NativeLibLoader.loadLibraryInternal(NativeLibLoader.java:163)
  5. System method java.lang.System.loadLibrary invoked at com.sun.glass.utils.NativeLibLoader.loadLibraryInternal(NativeLibLoader.java:177)
  6. Aborting stand-alone image build due to loading native libraries without configuration.
  7. at com.oracle.svm.hosted.FallbackFeature.afterAnalysis(FallbackFeature.java:293)
  8. ...

如何配置使用本地库?在Native Image编译工具中没有相应的选项,也没有我可以找到的任何相关提及。我已经成功地使用Native Image工具编译了其他项目,这意味着这个问题与JavaFX有关。

英文:

I have a JavaFX project, and would like to compile it into a Linux binary using the GraalVM Java virtual machine and the associated Native-Image tool. I am using GraalVM Java 11 Version 20.1.0, and the Native Image Maven plugin, that is added through Maven, to achieve this.

  1. &lt;plugin&gt;
  2. &lt;groupId&gt;com.oracle.substratevm&lt;/groupId&gt;
  3. &lt;artifactId&gt;native-image-maven-plugin&lt;/artifactId&gt;
  4. &lt;version&gt;19.2.1&lt;/version&gt;
  5. &lt;configuration&gt;
  6. &lt;mainClass&gt;sample.NewMain&lt;/mainClass&gt;
  7. &lt;imageName&gt;sample&lt;/imageName&gt;
  8. &lt;buildArgs&gt;
  9. -H:ReflectionConfigurationFiles=/home/user/Documents/Projects/TestProject/src/main/java/sample/reflect-config.json -H:+ReportExceptionStackTraces
  10. &lt;/buildArgs&gt;
  11. &lt;/configuration&gt;
  12. &lt;executions&gt;
  13. &lt;execution&gt;
  14. &lt;goals&gt;
  15. &lt;goal&gt;native-image&lt;/goal&gt;
  16. &lt;/goals&gt;
  17. &lt;phase&gt;package&lt;/phase&gt;
  18. &lt;/execution&gt;
  19. &lt;/executions&gt;
  20. &lt;/plugin&gt;

Originally, I got an error stating Warning: Aborting stand-alone image build due to reflection use without configuration. I used the Native Image tracing agent to generate config files for reflection,
which I pass into the compiler plugin like this:
-H:ReflectionConfigurationFiles=/home/user/Documents/Projects/TestProject/src/main/java/sample/reflect-config.json -H:+ReportExceptionStackTraces

I also have stack trace exception reporting turned on.

Now, when I attempt to compile to a native image, I get the following error to do with the usage of native libraries:

  1. Warning: System method java.lang.System.loadLibrary invoked at com.sun.glass.utils.NativeLibLoader.loadLibraryInternal(NativeLibLoader.java:163)
  2. Warning: System method java.lang.System.loadLibrary invoked at com.sun.glass.utils.NativeLibLoader.loadLibraryInternal(NativeLibLoader.java:177)
  3. Warning: Aborting stand-alone image build due to loading native libraries without configuration.
  4. com.oracle.svm.hosted.FallbackFeature$FallbackImageRequest: System method java.lang.System.loadLibrary invoked at com.sun.glass.utils.NativeLibLoader.loadLibraryInternal(NativeLibLoader.java:163)
  5. System method java.lang.System.loadLibrary invoked at com.sun.glass.utils.NativeLibLoader.loadLibraryInternal(NativeLibLoader.java:177)
  6. Aborting stand-alone image build due to loading native libraries without configuration.
  7. at com.oracle.svm.hosted.FallbackFeature.afterAnalysis(FallbackFeature.java:293)
  8. at com.oracle.svm.hosted.NativeImageGenerator.lambda$runPointsToAnalysis$9(NativeImageGenerator.java:741)
  9. at com.oracle.svm.hosted.FeatureHandler.forEachFeature(FeatureHandler.java:70)
  10. at com.oracle.svm.hosted.NativeImageGenerator.runPointsToAnalysis(NativeImageGenerator.java:741)
  11. at com.oracle.svm.hosted.NativeImageGenerator.doRun(NativeImageGenerator.java:538)
  12. at com.oracle.svm.hosted.NativeImageGenerator.lambda$run$0(NativeImageGenerator.java:451)
  13. at java.base/java.util.concurrent.ForkJoinTask$AdaptedRunnableAction.exec(ForkJoinTask.java:1407)
  14. at java.base/java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:290)
  15. at java.base/java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(ForkJoinPool.java:1020)
  16. at java.base/java.util.concurrent.ForkJoinPool.scan(ForkJoinPool.java:1656)
  17. at java.base/java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1594)
  18. at java.base/java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:177)

How can I configure the use of native libraries? There is no option for this within the Native Image compiler tool, nor any mention of it that I can find anywhere.

I have managed to compile other projects with the Native Image tool, which means that this problem is JavaFX related.

答案1

得分: 3

这样不起作用。 您将需要使用Gluon的client-maven-plugin(https://github.com/gluonhq/client-maven-plugin)来实现此目的。 它提供了编译的Java和JavaFX库的特殊版本,以使此工作正常。 请仔细按照说明操作。 然后它将正常工作。 我经常在使用它。

英文:

This does not work that way. You will have to use Gluons client-maven-plugin https://github.com/gluonhq/client-maven-plugin for this purpose. It provides a special version of compiled Java and JavaFX libraries to make this work. Follow the instructions closely. Then it will work. I am using it on a regular basis.

huangapple
  • 本文由 发表于 2020年8月15日 04:10:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/63419433.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定