gpt4 book ai didi

Graal 编译的 JavaFX 应用程序中的 java.lang.ClassNotFoundException

转载 作者:行者123 更新时间:2023-12-04 10:48:26 25 4
gpt4 key购买 nike

我正在开发一个可以编译成 native 应用程序的小型 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 文件和一个 native 二进制文件(在本例中为 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。但是,生成的 native 二进制文件从终端作为 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 端是否存在某种静默编译错误或缺少参数?

最佳答案

问题是 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 的类的示例> 出于同样的原因添加。

关于Graal 编译的 JavaFX 应用程序中的 java.lang.ClassNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59592940/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com