如何使用带有 JavaFX 的 GraalVM 在 Maven 中编译原生图像?

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 插件来实现此目的。

<plugin>
    <groupId>com.oracle.substratevm</groupId>
    <artifactId>native-image-maven-plugin</artifactId>
    <version>19.2.1</version>
    <configuration>
        <mainClass>sample.NewMain</mainClass>
        <imageName>sample</imageName>
        <buildArgs>
            -H:ReflectionConfigurationFiles=/home/user/Documents/Projects/TestProject/src/main/java/sample/reflect-config.json -H:+ReportExceptionStackTraces
        </buildArgs>
    </configuration>
    <executions>
        <execution>
            <goals>
                <goal>native-image</goal>
            </goals>
            <phase>package</phase>
        </execution>
    </executions>
</plugin>

最初,我收到一条错误消息,指出 Warning: Aborting stand-alone image build due to reflection use without configuration. 我使用本机图像跟踪代理生成用于反射的配置文件, 我像这样传递给编译器插件: -H:ReflectionConfigurationFiles=/home/user/Documents/Projects/TestProject/src/main/java/sample/reflect-config.json -H:+ReportExceptionStackTraces

我还打开了堆栈跟踪异常报告。

现在,当我尝试编译为本机映像时,出现以下与使用本机库有关的错误:

Warning: System method java.lang.System.loadLibrary invoked at com.sun.glass.utils.NativeLibLoader.loadLibraryInternal(NativeLibLoader.java:163)
Warning: System method java.lang.System.loadLibrary invoked at com.sun.glass.utils.NativeLibLoader.loadLibraryInternal(NativeLibLoader.java:177)
Warning: Aborting stand-alone image build due to loading native libraries without configuration.
com.oracle.svm.hosted.FallbackFeature$FallbackImageRequest: System method java.lang.System.loadLibrary invoked at com.sun.glass.utils.NativeLibLoader.loadLibraryInternal(NativeLibLoader.java:163)
System method java.lang.System.loadLibrary invoked at com.sun.glass.utils.NativeLibLoader.loadLibraryInternal(NativeLibLoader.java:177)
Aborting stand-alone image build due to loading native libraries without configuration.
    at com.oracle.svm.hosted.FallbackFeature.afterAnalysis(FallbackFeature.java:293)
    at com.oracle.svm.hosted.NativeImageGenerator.lambda$runPointsToAnalysis(NativeImageGenerator.java:741)
    at com.oracle.svm.hosted.FeatureHandler.forEachFeature(FeatureHandler.java:70)
    at com.oracle.svm.hosted.NativeImageGenerator.runPointsToAnalysis(NativeImageGenerator.java:741)
    at com.oracle.svm.hosted.NativeImageGenerator.doRun(NativeImageGenerator.java:538)
    at com.oracle.svm.hosted.NativeImageGenerator.lambda$run[=11=](NativeImageGenerator.java:451)
    at java.base/java.util.concurrent.ForkJoinTask$AdaptedRunnableAction.exec(ForkJoinTask.java:1407)
    at java.base/java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:290)
    at java.base/java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(ForkJoinPool.java:1020)
    at java.base/java.util.concurrent.ForkJoinPool.scan(ForkJoinPool.java:1656)
    at java.base/java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1594)
    at java.base/java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:177)

如何配置原生库的使用? Native Image 编译器工具中没有这方面的选项,我在任何地方都找不到它。

我已经成功地使用 Native Image 工具编译了其他项目,这意味着此问题与 JavaFX 相关。

这样不行。为此,您必须使用 Gluons client-maven-plugin https://github.com/gluonhq/client-maven-plugin。它提供了一个特殊版本的已编译 Java 和 JavaFX 库来完成这项工作。严格按照说明进行操作。然后它会起作用。我经常使用它。