"class file has wrong version 54.0, should be 52.0" while compiling javaFX project

14,535

Solution 1

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

Solution 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 ...
Share:
14,535
Adam Stafiej
Author by

Adam Stafiej

Updated on June 07, 2022

Comments

  • Adam Stafiej
    Adam Stafiej almost 2 years

    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.