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

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

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插件的配置代码:

<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.” 我使用了Native Image追踪代理来生成反射的配置文件,并将其传递给编译器插件,如下所示:
-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)
	...

如何配置使用本地库?在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.

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

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$9(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$0(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)

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:

确定