JavaFX11和Intellij

时间:2018-12-13 22:30:35

标签: java intellij-idea javafx openjfx

我按照此链接中的说明进行操作 https://openjfx.io/openjfx-docs/#install-javafx 当我运行javafx示例时,出现此错误

Error:(3, 26) java: cannot access javafx.application.Application
  bad class file: C:\Program Files\Java\OpenJDK\javafx-sdk-11.0.1\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.

当我转到“项目结构”>“项目/模块”时,JDK设置为JDK11 VM选项:

--module-path C:\Program Files\Java\OpenJDK\javafx-sdk-11.0.1\lib --add-modules=javafx.controls,javafx.fxml

我尝试去设置>构建,执行,部署>编译器> Java编译器 项目字节码版本设置为8,我将其更改为11,第一个错误消失了,但出现了新错误

Error:java: invalid target release: 11

我正在互联网上搜索,有人说去.idea\compiler.xml并将target设置为11,但对我来说已经设置为11,但是我得到了错误提示

2 个答案:

答案 0 :(得分:0)

转到文件>项目结构>模块>源

将“语言级别”设置为11(如果“ 11不可用”设置为“ X实验功能”)

Image

答案 1 :(得分:0)

我刚刚遇到错误,也找不到合适的解决方案,所以我自己想通了。

问题位置: pom.xml - maven 文件

有时问题可能发生在不同的 JavaFX 实体上,例如 FXMLLoader 或 EventHandler:

  1. java: cannot access javafx.fxml.FXMLLoader bad class file: ... class file has wrong version 55.0, should be 53.0
  2. java: cannot access javafx.event.EventHandler bad class file: /Users/john/.m2/repository/org/openjfx/javafx-base/12/javafx-base-12-mac.jar!/javafx/event/EventHandler.class class file has wrong version 55.0, should be 53.0

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>hkr</groupId>
    <artifactId>CommunicationTest</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>9</maven.compiler.source>
        <maven.compiler.target>9</maven.compiler.target>
    </properties>

不正确(以前的版本):

<?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>hkr</groupId>
    <artifactId>CommunicationTest</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

问题出在 properties 标签中,目标版本低于 JDK 版本 (9.0.1),这在我的情况下导致了 "class file has wrong version 55.0, should be 53.0" 的问题。

我只是更改为目标 9 而不是 1.8 并且它得到了修复。