【发布时间】: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>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