我的 JavaFX 应用程序从源代码运行得非常好,但是当我编译为单个 jar 文件时,出现错误:
Error: JavaFX runtime components are missing, and are required to run this application.
我使用 Maven 作为我的存储库管理器,并且我的 Maven 安装成功。
注意:在我的 IntelliJ 构建工件中,我可以看到 IntelliJ 包含 JavaFX 及其所有库
最佳答案
a) 对于我的 Maven 项目,诀窍是使用一个不继承自 javafx.application.Application 的额外启动类:
public class GUIStarter {
public static void main(final String[] args) {
GUI.main(args);
}
}
JavaFx 依赖项包含在我的 pom.xml 中,如下所示:
<!-- JavaFx -->
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-base</artifactId>
<version>12</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>12</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-graphics </artifactId>
<version>12</version>
<classifier>win</classifier>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-fxml</artifactId>
<version>12</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-web</artifactId>
<version>12</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-media</artifactId>
<version>12</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-swing</artifactId>
<version>12</version>
</dependency>
为了在构建中包含 fxml 文件,您可能需要将它们定义为资源:
!-- define JavaFx files as resources to include them in the jar -->
<resource>
<directory>${basedir}/src/main/java/foo/baa/view</directory>
<filtering>false</filtering>
<targetPath>foo/baa/view</targetPath>
</resource>
b) 作为替代方案,可以使用额外的 Maven 插件“javafx-maven-plugin”来构建 javafx 应用程序,请参阅示例项目 https://openjfx.io/openjfx-docs/#maven (这对我不起作用。我有几个 pom 文件,想要在子项目中引用 javafx 依赖项。javafx-maven-plugin 找不到我的主类。)
关于javafx-11 - 如何修复 "Error: JavaFX runtime components are missing, and are required to run this application"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56894627/