【发布时间】:2021-12-13 23:25:24
【问题描述】:
我有一个通过 maven 添加了 Spring Boot 和 JavaFX 的 Java 项目。代码编译,甚至我可以在计算机中没有 JavaFX SDK 的情况下执行 fat jar。但是当我尝试在 IntelliJ 中执行它时会导致
Error: JavaFX runtime components are missing, and are required to run this application
我在很多问题中都看到了这个输出,在大多数情况下,jar 根本没有构建或代码编译失败。
但在这种情况下,mvn package 可以正常工作,我可以使用java -jar <jar_name> 执行 JAR,以消除我可能在某处安装了 javafx sdk 的事实,我在仅安装了 JRE 的 VM 中尝试了它.
pom.xml
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>11.0.2</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-fxml</artifactId>
<version>11.0.2</version>
</dependency>
至于插件spring-boot-maven-plugin 和maven-compiler-plugin。
尝试的解决方案
--1--
我尝试了说添加的解决方案
<plugin>
<groupId>org.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>0.0.6</version>
<executions>
<execution>
<id>default-cli</id>
<configuration>
<mainClass>com.example.demofx.Starter</mainClass>
</configuration>
</execution>
</executions>
</plugin>
但它的作用是增加运行的能力:mvn clean javafx:run
使用 IntelliJ 执行的需要是调试代码,因为使用 souts 进行调试效率不高。
--2--
尝试使用 module-info.jar 进行模块构建并关注
module com.example.demofx {
requires javafx.controls;
requires javafx.fxml;
// all other required modules including spring
opens com.example.demofx to javafx.fxml;
exports com.example.demofx;
}
这可能有效,但由于一些旧的依赖项无法在模块化构建中正常工作,这会导致代码库发生大量重大更改。
编辑:
- 没提环境
- JDK - 11.0.8
- IntelliJ IDEA - 2021.2.2
- 已尝试添加第二个解决方案。
【问题讨论】:
-
你的“主要”课程是什么?您是否尝试使用扩展应用程序的类来启动它?我知道有一种使用非模块化 javafx 的解决方法,您需要使用 main 创建一个单独的类,然后从那里启动您的应用程序。
-
你检查过这里吗? github.com/openjfx/samples#IntelliJ-Non-Modular-Samples你用的是什么版本的javafx?
-
@matt 至于您发送的 lib,我目前在 pom 中具有相同的依赖项和插件,并且 Main 类也与非模块化示例中的类似。正如我在打包工作后提到的代码。只是 IntelliJ 无法将该项目检测为 JavaFX 项目。但是如果我创建了一个新项目并选择了新的 javafx 项目选项,那么具有相同 pom.xml 的相同代码可以工作。
标签: java maven intellij-idea javafx