javamavenintellij-ideajavafx

Newer JDK version is used but not recognized in JavaFX?


This is my maven build

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.10.1</version>
            </plugin>
            <plugin>
                <groupId>org.openjfx</groupId>
                <artifactId>javafx-maven-plugin</artifactId>
                <version>0.0.8</version>
                <executions>
                    <execution>
                        <!-- Default configuration for running with: mvn clean javafx:run -->
                        <id>default-cli</id>
                        <configuration>
                            <mainClass>org.abos.gs.gui.fx.FxMenu</mainClass>
                            <launcher>app</launcher>
                            <jlinkZipName>app</jlinkZipName>
                            <jlinkImageName>app</jlinkImageName>
                            <noManPages>true</noManPages>
                            <stripDebug>true</stripDebug>
                            <noHeaderFiles>true</noHeaderFiles>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

Maven command is clean javafx:run. Both the pom and the IntelliJ maven properties are running on Java 21.

    <properties>
        <maven.compiler.source>21</maven.compiler.source>
        <maven.compiler.target>21</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

IntelliJ settings

Nevertheless I get this error message

Error: LinkageError occurred while loading main class org.abos.gs.gui.fx.FxMenu
    java.lang.UnsupportedClassVersionError: org/abos/gs/gui/fx/FxMenu has been compiled by a more recent version of the Java Runtime (class file version 65.0), this version of the Java Runtime only recognizes class file versions up to 62.0

which is weird because the console starts with /usr/lib/jvm/jdk-21.0.1/bin/java

Any idea what could be wrong?

EDIT: At least setting the Java version to 17 in the pom and checking the box "Delegate IDE build/run actions to Maven" makes the program compile and run, but I want to run it on Java 21.

EDIT2: Setting a JAVA_HOME to my JDK 21 solved the issue. I didn't need it before and I think this is a bug in maven or IntelliJ to not use the JDK I explicitly specified.


Solution

  • Solutions

    You can tell the JavaFX Maven Plugin which JDK installation to use for linking by either:

    OR

    These settings are independent of the Java version used to execute the Maven JavaFX Plugin javafx:jlink goal.

    Toolchains plugin

    The Maven JavaFX Plugin is Maven toolchain aware, allowing selection of the toolchain (JDK/jlink/etc) using the maven-toolchain-plugin. (I haven't verified or tested that).

    The Maven JavaFX Plugin has code to support maven toolchains. When a toolchain is configured, it takes precedence over a JAVA_HOME environment variable configuration.

    What the toolchains plugin does:

    The Toolchains Plugins enables sharing tool configuration across plugins; for example, to make sure that plugins like compiler, surefire, and javadoc all use the same JDK for execution, independently from JRE used to run Maven itself.

    FAQ

    Setting a JAVA_HOME to my JDK 21 solved the issue. I didn't need it before and I think this is a bug in Maven or IntelliJ to not use the JDK I explicitly specified.

    I don't think it is a bug, I think it is an undocumented feature for the JavaFX maven plugin (which should be documented IMO).

    I have found this feature quite annoying at times in the past when developing JavaFX applications in Idea, because it means that you need to have to either hardcode the jlink executable path in the Maven JavaFX Plugin configuration or rely on an external JAVA_HOME environment variable, just to get the linkage settings correct.

    How the Maven JavaFX Plugin works

    Reviewing the Maven JavaFX Plugin source shows that the plugin works out where to find the JDK from JAVA_HOME if it is defined. It is a generic lookup, so it works for all goals of the plugin (e.g. both the javafx:jlink and javafx:run goals).

    Alternately, the lookup based on the jlinkExecutable configuration setting is documented in the plugin options.

    JavaFX Maven Plugin Version used

    The information in this answer relates to version 0.0.8 of the JavaFX Maven plugin and may not apply to other versions.

    Related

    General info on solving linkage issues with Java, (does not address the Maven JavaFX Plugin configuration and operation directly):