Ahmed Bayunes
Ahmed Bayunes

Reputation: 11

JavaFX11 and Intellij

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

Upvotes: 1

Views: 3798

Answers (2)

Dan
Dan

Reputation: 593

I have just encountered the error, could not find proper solution either, so I figured out myself.

Problem location: pom.xml - maven file

Sometimes issue can happen with different JavaFX entity, for instance with FXMLLoader or 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

Fixed version of pom.xml file:

<?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>

Incorrect (previous verison):

<?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>

The issue is in properties tag, the target version was below JDK version (9.0.1), which was causing in my case a problem of "class file has wrong version 55.0, should be 53.0".

I simply changed to target 9 instead of 1.8 and it got fixed.

Upvotes: 0

AUPMA
AUPMA

Reputation: 304

Go To File > Project Structure > Modules > Source

Set Language Level to 11 (if 11 not Available set to "X-Experimental Features")

Image

Upvotes: 1

Related Questions