我正在研究一个小型OpenJFX项目,该项目可以编译成本机应用程序。这是源代码相关部分的示例:
Main.java
package app;
public class Main {
public static void main(String[] args) {
App.main(args);
}
}
App.java
package app;
import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class App extends Application {
private static Scene scene;
@Override
public void start(Stage stage) throws Exception {
scene = new Scene(loadFXML("MainPanel"));
stage.setScene(scene);
stage.show();
}
static void setRoot(String fxml) throws IOException {
scene.setRoot(loadFXML(fxml));
}
private static Parent loadFXML(String fxml) throws IOException {
FXMLLoader fxmlLoader = new FXMLLoader(
App.class.getResource(fxml + ".fxml"));
return fxmlLoader.load();
}
public static void main(String[] args) {
launch();
}
}
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>app</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>0.0.3</version>
<configuration>
<mainClass>app.App</mainClass>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>app.Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>app.Main</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.graalvm.nativeimage</groupId>
<artifactId>native-image-maven-plugin</artifactId>
<version>19.3.0</version>
<executions>
<execution>
<goals>
<goal>native-image</goal>
</goals>
<phase>package</phase>
</execution>
</executions>
<configuration>
<skip>false</skip>
<imageName>app</imageName>
<buildArgs>
--no-fallback
</buildArgs>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>13</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-fxml</artifactId>
<version>13</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-graphics</artifactId>
<version>13</version>
<type>jar</type>
<classifier>win</classifier>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-graphics</artifactId>
<version>13</version>
<type>jar</type>
<classifier>linux</classifier>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-graphics</artifactId>
<version>13</version>
<type>jar</type>
<classifier>mac</classifier>
</dependency>
<dependency>
<groupId>org.graalvm.sdk</groupId>
<artifactId>graal-sdk</artifactId>
<version>19.3.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
该项目成功编译为两个.jar文件和一个本地二进制文件(在此特定情况下,为Linux x86_64 Glibc ELF二进制文件)。非独立的.jar(稍后由GraalVM编译)可以使用java --module-path /usr/share/openjfx/lib --add-modules javafx.controls,javafx.fxml -jar target/app-1.0-SNAPSHOT.jar
成功执行。但是,从终端作为target/app
启动的生成的本机二进制文件出现以下错误:
Exception in thread "main" java.lang.RuntimeException: java.lang.ClassNotFoundException: app.App
at javafx.application.Application.launch(Application.java:304)
at app.App.main(App.java:32)
at app.Main.main(Main.java:6)
Caused by: java.lang.ClassNotFoundException: app.App
at com.oracle.svm.core.hub.ClassForNameSupport.forName(ClassForNameSupport.java:60)
at java.lang.Class.forName(DynamicHub.java:1197)
at javafx.application.Application.launch(Application.java:292)
... 2 more
我在做错什么吗,GraalVM方面是否存在某种无提示的编译错误或缺少参数?
答案 0 :(得分:2)
问题在于GraalVM无法编译包含具有反射unless told beforehand的类的独立二进制文件。应该可以使用native-image-maven-plugin
进行设置,但是我改用了Gluon Client,这意味着在删除不再有用的东西之后,我对pom.xml
作了以下补充:
<pluginRepositories>
<pluginRepository>
<id>gluon-releases</id>
<url>https://nexus.gluonhq.com/nexus/content/repositories/releases/</url>
</pluginRepository>
</pluginRepositories>
<build>
<plugins>
<plugin>
<groupId>com.gluonhq</groupId>
<artifactId>client-maven-plugin</artifactId>
<version>0.1.10</version>
<configuration>
<mainClass>app.App</mainClass>
<reflectionList>
<list>javafx.fxml.FXMLLoader</list>
<list>app.SomeCustomClass</list>
</reflectionList>
</configuration>
</plugin>
</plugins>
</build>
请注意,app.SomeCustomClass
是一个类的示例,该类返回了类似的java.lang.ClassNotFoundException
错误,并且出于相同的原因添加了javafx.fxml.FXMLLoader
。