【问题标题】:"class file has wrong version 54.0, should be 52.0" while compiling javaFX project编译 javaFX 项目时“类文件的版本错误 54.0,应该是 52.0”
【发布时间】:2019-07-10 17:22:30
【问题描述】:

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

【问题讨论】:

  • 您是否确认您使用的是 Java 11? java -version 的输出是什么?
  • @Slaw java --version 在项目目录的终端中输出 java 10。这是检查它的正确方法吗? C:\Users\qaze\IdeaProjects\hellofx2&gt;java --version java 10.0.1 2018-04-17 Java(TM) SE Runtime Environment 18.3 (build 10.0.1+10) Java HotSpot(TM) 64-Bit Server VM 18.3 (build 10.0.1+10, mixed mode) 但是运行配置状态为 JRE 11。
  • @AdamStafiej 配置可能会请求它,但运行时(JRE 10)不支持它。你需要一个真正支持它的运行时。
  • 顺便说一句:即使使用相同的 JRE 版本,类文件版本也可能不同 - 例如较新版本的 version10 具有与旧版本不同的类文件版本(这实际上有点奇怪,但活得很好并不容易..)
  • 我已经更改了它,它是openjdk 11 2018-09-25,javac 是javac 11。有趣的是,错误只发生了一点变化:Error:(3, 20) java: cannot access javafx.event.ActionEvent bad class file: C:\Users\qaze\.m2\repository\org\openjfx\javafx-base\11.0.2\javafx-base-11.0.2-win.jar(javafx/event/ActionEvent.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. 我应该尝试让一切都在 java 10 上运行吗?

标签: java maven intellij-idea javafx


【解决方案1】:

我有这个错误,我通过添加修复它

<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 文件

【讨论】:

    【解决方案2】:

    我过去也遇到过类似的问题。问题是机器上安装了java8并被maven使用,tough java11是默认的。解决方案是确保 JAVA_HOME 指向 java11 安装。可以运行mvn -X查看哪个jvm用来运行maven,哪个javac用来编译代码:

    $ mvn  -X clean package
    Apache Maven 3.5.4 (Red Hat 3.5.4-5)
    Maven home: /usr/share/maven
    Java version: 1.8.0_272, vendor: Red Hat, Inc., runtime: /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.272.b10-4.el8.x86_64/jre
    Default locale: en_US, platform encoding: ANSI_X3.4-1968
    OS name: "linux", version: "4.18.0-240.el8.x86_64", arch: "amd64", family: "unix"
    
    ...
    ... java.home=/usr/lib/jvm/java-11-openjdk-11.0.9.11-3.el8.x86_64 ...
    

    【讨论】:

      【解决方案3】:

      我遇到了同样的问题,

      只需删除org文件夹即可解决。

      C:\Temp\JavaCABuild\org\openqa\selenium\support\ui\Duration.class

      【讨论】:

        【解决方案4】:

        您可能只需要转到 File|ProjectStructure|Project -> Project language Level => 11

        如果您进行顶级设置,它会为我修复它,我认为这是最好的解决方案, 如果不是 11、6 或任何低于 11 的东西,javafx 中的某些东西可能不起作用

        如果您在其他编辑器中遇到此问题,那么 Intellij 您可能还需要找到设置并将其设置为 11。

        这是设置的图像:

        【讨论】:

          【解决方案5】:

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

          就我而言,我已将 Scala 版本设置为 2.13.4,但构建失败。 恢复到 Scala v2.12.10 修复了它。

          【讨论】:

            猜你喜欢
            • 2015-03-26
            • 2015-07-06
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2014-03-31
            • 2015-11-18
            • 1970-01-01
            • 2022-01-23
            相关资源
            最近更新 更多