【问题标题】:JavaFX11 and IntellijJavaFX11 和 Intellij
【发布时间】:2019-05-15 05:01:48
【问题描述】:

我按照此链接中的说明进行操作 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 虚拟机选项:

--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 并将目标设置为 11,但对我来说它已经设置为 11,但我得到了错误

【问题讨论】:

  • 您确定您的 JDK 设置为正确的版本吗?
  • 是的,我确定它设置为 JDK 11,但它仍然说 javac 8 用于编译
  • 这意味着JDK没有设置为11,如果它仍然使用8来编译...
  • 如果它使用的是 jdk 8 那么我如何阻止它使用它,因为在项目设置中我确实将它设置为 jdk 11
  • 您是否使用 Maven 或 Gradle 之类的工具来处理构建?

标签: java intellij-idea javafx openjfx


【解决方案1】:

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

将语言级别设置为 11(如果 11 不可用,则设置为“X-Experimental Features”)

Image

【讨论】:

    【解决方案2】:

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

    问题位置: 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" 的问题。

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

    【讨论】:

      猜你喜欢
      • 2019-03-01
      • 2019-11-22
      • 2019-04-26
      • 1970-01-01
      • 2019-05-30
      • 2015-04-23
      • 1970-01-01
      • 2022-08-09
      • 2019-10-01
      相关资源
      最近更新 更多