java - 编译 javaFX 项目时出现 "class file has wrong version 54.0, should be 52.0"

标签 java maven intellij-idea javafx

我已经尝试使用 Maven 构建示例 javaFX 项目,但我不断收到以下错误。

Error:(3, 26) java: cannot access javafx.application.Application
  bad class file: C:\Program Files\Java\javafx-sdk-11.0.2\lib\javafx.graphics.jar(javafx/application/Application.class)
    class file has wrong version 54.0, should be 52.0
    Please remove or make sure it appears in the correct subdirectory of the classpath.

我从这个链接中获取了项目:

https://github.com/openjfx/samples/tree/master/IDE/IntelliJ/Non-Modular/Maven

我使用 JRE 11。 这是我的 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>org.openjfx</groupId>
    <artifactId>hellofx</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>hellofx</name>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <mainClass>org.openjfx.MainApp</mainClass>
    </properties>

    <organization>
        <!-- Used as the 'Vendor' for JNLP generation -->
        <name>Your Organisation</name>
    </organization>

    <dependencies>
        <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>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <release>11</release>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.6.0</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>java</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <mainClass>org.openjfx.MainApp</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

顺便说一句,从 IntelliJ 构建示例 JavaFX 应用程序并将 javafx-sdk-11.0.2 添加为依赖项会产生完全相同的错误。

最佳答案

我遇到了这个错误,我通过添加

修复了它
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>10</source>
                <target>10</target>
                <encoding>UTF-8</encoding>
            </configuration>
        </plugin>
        <plugin>
            <!-- Build an executable JAR -->
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>3.1.0</version>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>lib/</classpathPrefix>
                        <mainClass>Main</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
    </plugins>
</build>

到pom文件

关于java - 编译 javaFX 项目时出现 "class file has wrong version 54.0, should be 52.0",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54728420/

相关文章:

maven - 为什么maven-site-plugin总是使用版本3.3?

intellij-idea - Intellij 无法识别 javax.mail 导入,但项目构建正常

java - 这是如何评价的? (犯罪)(getListAdapter()).getItem(位置)

java - 运行我的应用程序时出现错误 "context initialization failed"

java - 如何编写具有泛型类类型的 java 方法?

java - 无法使用 Ubuntu 在 Jenkins 上运行 Maven

java - 是否存在依赖 Artifact 的 Maven "compiler-only"范围

java - 无法在 IntelliJ Ultimate 中使用外部依赖项(由 Maven 安装)

intellij-idea - 如何重定向 IntelliJ 的配置路径?

java - 不会增加半径,但应该