有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

在编译javaFX项目时,java“类文件的版本54.0错误,应该是52.0”

我曾尝试用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作为依赖项会产生完全相同的错误


共 (3) 个答案

  1. # 1 楼答案

    我也有同样的问题

    只需删除组织文件夹即可解决此问题

    C:\Temp\JavaCABuild\org\openqa\selenium\support\ui\Duration。阶级

  2. # 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文件

  3. # 3 楼答案

    对于在运行Play 2.x应用程序+sbt时遇到此问题的任何人,请尝试上面列出的解决方案,并查看Scala版本是否已设置为2.12.x

    在我的例子中,我将Scala版本设置为2.13.4,但构建失败。 恢复到Scala v2.12.10修复了它