1

I followed the instruction in this link https://openjfx.io/openjfx-docs/#install-javafx and when i run javafx sample i get this error

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.

and when i go to Project Structure > Project/Modules the JDK is set to JDK11 VM option:

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

I tried to go to Settings > Build, Execution, Deployment > Compiler > Java Compiler Project bytecode version was set to 8 and i changed it to 11 and the first error was gone but i got new error

Error:java: invalid target release: 11

I was searching on the internet and somebody said go to .idea\compiler.xml and set target to 11 but it's already set to 11 for me but i get the error

4

2 回答 2

1

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

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

图片

于 2018-12-27T07:08:40.597 回答
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>

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

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

于 2021-04-23T01:25:06.793 回答