Maven GluonFX项目构建可执行文件时FXML加载失败求助
GluonFX原生构建后FXML加载失败问题
问题现象
- 通过x64 Native Tools执行
mvn gluonfx:build完成构建,但运行生成的可执行文件时FXML未加载 - IntelliJ中执行
mvn gluonfx:run一切正常 - x64 Native Tools中执行
mvn gluonfx:nativerun报错,提示primary.fxml第37行加载异常,但同环境下mvn gluonfx:run可正常运行,问题锁定在Native运行模式的FXML加载环节
相关文件信息
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> <groupId>com.knnapplication</groupId> <artifactId>knn-application</artifactId> <version>1.0</version> <packaging>jar</packaging> <name>KNN-Application</name> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.release>11</maven.compiler.release> <javafx.version>19</javafx.version> <attach.version>4.0.16</attach.version> <gluonfx.plugin.version>1.0.15</gluonfx.plugin.version> <javafx.plugin.version>0.0.8</javafx.plugin.version> <mainClassName>com.knnapplication.KNNApplication</mainClassName> </properties> <dependencies> <dependency> <groupId>org.openjfx</groupId> <artifactId>javafx-controls</artifactId> <version>${javafx.version}</version> </dependency> <dependency> <groupId>org.openjfx</groupId> <artifactId>javafx-fxml</artifactId> <version>${javafx.version}</version> </dependency> <dependency> <groupId>com.gluonhq</groupId> <artifactId>charm-glisten</artifactId> <version>6.1.1</version> </dependency> <dependency> <groupId>com.gluonhq.attach</groupId> <artifactId>display</artifactId> <version>${attach.version}</version> </dependency> <dependency> <groupId>com.gluonhq.attach</groupId> <artifactId>lifecycle</artifactId> <version>${attach.version}</version> </dependency> <dependency> <groupId>com.gluonhq.attach</groupId> <artifactId>statusbar</artifactId> <version>${attach.version}</version> </dependency> <dependency> <groupId>com.gluonhq.attach</groupId> <artifactId>storage</artifactId> <version>${attach.version}</version> </dependency> <dependency> <groupId>com.gluonhq.attach</groupId> <artifactId>util</artifactId> <version>${attach.version}</version> </dependency> </dependencies> <repositories> <repository> <id>Gluon</id> <url>https://nexus.gluonhq.com/nexus/content/repositories/releases</url> </repository> </repositories> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> </plugin> <plugin> <groupId>org.openjfx</groupId> <artifactId>javafx-maven-plugin</artifactId> <version>${javafx.plugin.version}</version> <configuration> <mainClass>${mainClassName}</mainClass> </configuration> </plugin> <plugin> <groupId>com.gluonhq</groupId> <artifactId>gluonfx-maven-plugin</artifactId> <version>${gluonfx.plugin.version}</version> <configuration> <target>${gluonfx.target}</target> <attachList> <list>display</list> <list>lifecycle</list> <list>statusbar</list> <list>storage</list> </attachList> <reflectionList> <list>com.knnapplication.views.PrimaryPresenter</list> <list>com.knnapplication.views.SecondaryPresenter</list> </reflectionList> <mainClass>${mainClassName}</mainClass> </configuration> </plugin> </plugins> </build> <profiles> <profile> <id>ios</id> <properties> <gluonfx.target>ios</gluonfx.target> </properties> </profile> <profile> <id>android</id> <properties> <gluonfx.target>android</gluonfx.target> </properties> </profile> </profiles> </project>
异常日志
[dom nov 13 20:38:42 CET 2022][INFO] [SUB] nov 13, 2022 8:38:42 PM com.sun.javafx.application.PlatformImpl startup [dom nov 13 20:38:42 CET 2022][INFO] [SUB] WARNING: Unsupported JavaFX configuration: classes were loaded from 'unnamed module @6e8dacdf' [dom nov 13 20:38:42 CET 2022][INFO] [SUB] nov 13, 2022 8:38:42 PM com.gluonhq.attach.util.Platform <clinit> [dom nov 13 20:38:42 CET 2022][INFO] [SUB] INFO: [Gluon Attach] System Property javafx.platform is not defined. Platform will be set to Platform.DESKTOP [dom nov 13 20:38:42 CET 2022][INFO] [SUB] IOException: javafx.fxml.LoadException: [dom nov 13 20:38:42 CET 2022][INFO] [SUB] /com/knnapplication/views/primary.fxml:37 [dom nov 13 20:38:42 CET 2022][INFO] [SUB]
primary.fxml第37行内容
第37行是TableView控件的定义代码:
<TableView fx:id="tableView" layoutX="14.0" layoutY="14.0" prefHeight="517.0" prefWidth="767.0">
解决方案建议
针对GluonFX原生模式下FXML加载失败的问题,可尝试以下步骤排查修复:
确认FXML资源被正确打包
- 检查项目中FXML文件是否位于
src/main/resources/com/knnapplication/views/目录下,确保Maven构建时能将资源正确复制到输出目录 - 在
gluonfx-maven-plugin配置中添加resourcesList,显式声明需要包含的FXML资源:<configuration> <!-- 其他配置 --> <resourcesList> <list>/com/knnapplication/views/primary.fxml</list> <list>/com/knnapplication/views/secondary.fxml</list> </resourcesList> </configuration>
- 检查项目中FXML文件是否位于
补充反射配置
- FXML加载时需要反射实例化控件和Presenter,除了已配置的Presenter类,还需添加FXML中用到的JavaFX控件类到
reflectionList,比如TableView、TableColumn等:<reflectionList> <list>com.knnapplication.views.PrimaryPresenter</list> <list>com.knnapplication.views.SecondaryPresenter</list> <list>javafx.scene.control.TableView</list> <list>javafx.scene.control.TableColumn</list> <!-- 添加其他用到的控件类 --> </reflectionList> - 也可以使用
gluonfx:reflect命令自动生成反射配置,避免手动遗漏
- FXML加载时需要反射实例化控件和Presenter,除了已配置的Presenter类,还需添加FXML中用到的JavaFX控件类到
检查FXML中引用的类型
- 确保FXML中所有自定义控件或类型都已正确配置反射,或者在代码中存在显式引用(避免GraalVM原生镜像构建时被裁剪)
更新GluonFX插件版本
- 当前使用的
gluonfx-maven-plugin版本为1.0.15,尝试升级到最新稳定版本,部分旧版本存在资源打包或反射处理的bug
- 当前使用的
调试原生镜像构建过程
- 执行
mvn gluonfx:build -X查看详细构建日志,确认资源是否被正确包含在原生镜像中 - 使用
gluonfx:runagent命令运行应用,自动收集运行时所需的反射、资源等配置,生成resource-config.json和reflect-config.json,然后将这些配置合并到插件中
- 执行
内容的提问来源于stack exchange,提问作者Luigi Daddario
相关产品推荐
相关产品推荐

