使用 maven 和 Java 17 启动 JavaFX 应用程序



My System:

  • 具有多个 jdk(1.8、16、17)
  • 拥有IntelliJ的Maven
  • 是一台窗户 10 机器

我想用maven启动我的JavaFX应用程序。为此,我使用插件菜单中的javafx:run按钮。

然后我收到有关我的jdk的错误,处于太低的水平(它告诉我我尝试用我的jdk-8执行我的 Main 方法),这是不正确的,因为每个jdk-specification都设置为jdk-17

请不要向我发送有关其他帖子的任何链接。我已经尝试过这些,但那里的步骤对我来说不起作用。

这是我得到的错误

  • Main has been compiled by a more recent version of the Java Runtime (class file version 61.0), this version of the Java Runtime only recognizes class file versions up to 52.0

    • 是的,我已经尝试更改 IntelliJs jdks、JAVA_HOME 变量和 maven 本身的运行配置
  • 有时我也会收到来自 maven 的错误,告诉我 maven 存储库中没有 JavaFX 版本 17(而是我工作场所的 maven 存储库)。有没有办法将我的 maven-repo 更改为默认的 maven 网站?

这是我的绒球.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>groupId</groupId>
<artifactId>M226a_miniproject</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<release>17</release>
<verbose>true</verbose>
<fork>true</fork>
<executable>C:/Users/[user.name]/.jdks/openjdk-17/bin/javac</executable>
<compilerVersion>1.3</compilerVersion>
</configuration>
</plugin>
<plugin>
<groupId>org.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>0.0.8</version>
<configuration>
<mainClass>ch.package.Main</mainClass>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.8.1</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>4.0.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.openjfx/javafx -->
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx</artifactId>
<version>17.0.0.1</version>
<type>pom</type>
</dependency>
<!-- https://mvnrepository.com/artifact/org.openjfx/javafx-graphics -->
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-graphics</artifactId>
<version>17.0.0.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.openjfx/javafx-fxml -->
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-fxml</artifactId>
<version>17.0.0.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.openjfx/javafx-controls -->
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>17.0.0.1</version>
</dependency>
</dependencies>
</project>

也许第二个插件中的<version>0.0.8</version>有问题?
另外:关于 JRE 无法识别类文件的错误确实毫无意义,因为即使是我的 maven 也使用我的 jdk-17 的自定义路径。

编辑:更新的绒球.xml

(修复了我的 Ubuntu 20.04 机器上的问题,但错误是一样的)

问题是,我只将 jdk-17 下载到 IntelliJ 中。

我只是通过运行sudo apt install openjdk-17-jdk来安装 jdk-17,它会安装 jdk 和
sudo update-alternatives --config java,它更新了最新 jdk 的 intex(我认为)。

最后一步是重新启动IntelliJ,我很好。

最新更新