java - 使用 Maven 和 Java 17 启动 JavaFX 应用程序

标签 java maven javafx java-17

我的系统:

  • 拥有多个 jdks(1.8、16、17)
  • 有 IntelliJ 的 Maven
  • 是 Windows 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 存储库(但我工作场所的 Maven 存储库)中没有 JavaFX 版本 17。有没有办法将我的 maven-repo 更改为默认的 maven 网站?

这是我的 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>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 的自定义路径。

编辑:更新了 pom.xml

最佳答案

在我的 Ubuntu 20.04 机器上修复了问题,但错误是相同的

问题是,我只将 JDK-17 下载到 IntelliJ 中,而不是整个系统。

因此,我只需通过运行以下命令安装并索引 JDK-17:

一个安装JDK

> sudo apt install openjdk-17-jdk

另一个将配置更新到最新的JDK

> sudo update-alternatives --config java

最后一步是重新启动 IntelliJ,一切顺利。

关于java - 使用 Maven 和 Java 17 启动 JavaFX 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69676622/

相关文章:

java - jars和其他文件的Maven打包

java - 应用程序启动方法错误中的异常。其他解决方案不起作用

css - CSS 中的 Java FX 发光效果

java - 在 TextField 中选择一个词

java - 在 MySQL 中处理 null

java - 将 Twirl 与 Spring MVC 结合使用

java - 插入 UUID 时十六进制数无效

java - 写入资源文件java?

java - EWSJavaAPI_1.2.jar 的 Maven 存储库

java - 获取本地 Android 项目文件的文件路径