8

I have tried building a sample javaFX project with maven and I keep getting the error below.

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.

I took the project from this link:

https://github.com/openjfx/samples/tree/master/IDE/IntelliJ/Non-Modular/Maven

I use JRE 11. This is my 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>

By the way building the sample JavaFX Application from IntelliJ and adding javafx-sdk-11.0.2 as a dependency yields the exact same error.

Adam Stafiej
  • 93
  • 1
  • 2
  • 10
  • Did you verify you're using Java 11? What is the output of `java -version`? – Slaw Feb 16 '19 at 22:45
  • @Slaw java --version in the terminal in project dir outputs java 10. Is this the correct way of checking it? `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)` However run config states JRE 11. – Adam Stafiej Feb 16 '19 at 22:51
  • @AdamStafiej config may request it but the runtime(JRE 10) does not support it. You need a runtime that actually supports it. – kai Feb 16 '19 at 22:55
  • and by the way: class file versions may be different even with the same JRE version - e.g. newer releases of version10 have different class file versions than older(this is actually a little odd but well live is not supposed to be easy .. ) – kai Feb 16 '19 at 22:59
  • I have changed it and it is `openjdk 11 2018-09-25` and javac is `javac 11`. Funny thing though is that the error has changed only a little: `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.` Should i just try to get everything working on java 10 instead? – Adam Stafiej Feb 16 '19 at 23:07
  • it's still the same: the JRE(java) matters not(rsp it does but isn't causing) the JDK(javac). Also I already pointed out, that class file versions may be different even within one JRE major version. – kai Feb 16 '19 at 23:19
  • see: https://bugs.java.com/bugdatabase/view_bug.do?bug_id=JDK-8191510. for introduction of classfile version 54 - its actually already in newer version 10 JRE's - yours is older. – kai Feb 16 '19 at 23:28
  • @kai Thank you. This gives me something to work with. I was beginning to wonder if I really need some kind of java from the future. – Adam Stafiej Feb 16 '19 at 23:37
  • no, a new jre10 seems to be sufficent regarding the present error, but I would rather use a jre11 with jfx 11(as recommended on the jfx page). – kai Feb 16 '19 at 23:40

5 Answers5

2

I had this error, I fixed it by adding

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>10</source>
                <target>10</target>
                <encoding>UTF-8</encoding>
            </configuration>
        </plugin>
        <plugin>
            <!-- Build an executable JAR -->
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>3.1.0</version>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>lib/</classpathPrefix>
                        <mainClass>Main</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
    </plugins>
</build>

to the pom file

2

I had a similar issue in the past. The problem was that java8 was installed on the machine and used by maven, tough java11 was the default. The solution was to ensure that JAVA_HOME points to java11 installation. You can run mvn -X to see which jvm is used to run maven and which javac is used to compile the code:

$ mvn  -X clean package
Apache Maven 3.5.4 (Red Hat 3.5.4-5)
Maven home: /usr/share/maven
Java version: 1.8.0_272, vendor: Red Hat, Inc., runtime: /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.272.b10-4.el8.x86_64/jre
Default locale: en_US, platform encoding: ANSI_X3.4-1968
OS name: "linux", version: "4.18.0-240.el8.x86_64", arch: "amd64", family: "unix"

...
... java.home=/usr/lib/jvm/java-11-openjdk-11.0.9.11-3.el8.x86_64 ...
kofemann
  • 4,217
  • 1
  • 34
  • 39
0

I had the same issue,

It is solved by just deleting org folder.

C:\Temp\JavaCABuild\org\openqa\selenium\support\ui\Duration.class

0

you might just need to go to File|ProjectStructure|Project -> Project language Level => 11

if you do the top setting it fixed it for me I think that this is the best solution, that being if there is instead of an eleven a six or anything lower than eleven, some things in javafx might not work

If you have this problem in an other Editor then Intellij you might also just need to find the setting and set it to 11.

here is an image of the setting:

setting to fix Problem in intellij

0

For anyone facing this issue while running a Play 2.x application + sbt, try the solutions listed above, and also see if Scala version has been set to 2.12.x.

In my case, I had set Scala version to 2.13.4 and the build was failing. Reverting to Scala v2.12.10 fixed it.

Akilan Arasu
  • 350
  • 1
  • 5
  • 17