Can't launch Payara Micro from payara-micro-maven-plugin, getting "unsupported JDK"

I put together a sample project to demonstrate the issue I’m having.

Basically, I’m trying to launch my app via payara micro’s maven plugin during development. Here is the pom.xml config:

<plugin>
                <groupId>fish.payara.maven.plugins</groupId>
                <artifactId>payara-micro-maven-plugin</artifactId>
                <version>1.4.0</version>
                <configuration>
                    <useUberJar>false</useUberJar>
                    <contextRoot>/myapp</contextRoot>
                    <payaraVersion>${version.payara}</payaraVersion>
                    <deployWar>false</deployWar>
                    <artifactItem>
                        <groupId>fish.payara.extras</groupId>
                        <artifactId>payara-micro</artifactId>
                        <version>${version.payara.micro}</version>
                    </artifactItem>
                    <javaCommandLineOptions>
                        <option>
                            <value>-Xdebug</value>
                        </option>
                    </javaCommandLineOptions>
                    <commandLineOptions>
                        <option>
                            <key>--autoBindHttp</key>
                        </option>
                        <option>
                            <key>--nocluster</key>
                        </option>
                        <option>
                            <key>--port</key>
                            <value>8095</value>
                        </option>
                        <option>
                            <key>--prebootcommandfile</key>
                            <value>${project.basedir}/src/main/resources/pre-boot-commands.txt</value>
                        </option>
                        <option>
                            <key>--postbootcommandfile</key>
                            <value>${project.basedir}/src/main/resources/post-boot-commands.txt</value>
                        </option>
                        <option>
                            <key>--deploy</key>
                            <value>${project.build.directory}/${project.build.finalName}</value>
                        </option>
                    </commandLineOptions>
                </configuration>
            </plugin>

The result is the following:

[2022-05-11T09:54:58.362-0400] [] [SEVERE] [] [org.eclipse.persistence.session./file:/path/to/payara-micro-plugin-group/payara-micro-plugin-example/target/payara-micro-plugin-example-1.0.0-SNAPSHOT/WEB-INF/classes/_MyAppPU.metadata] [tid: _ThreadID=1 _ThreadName=main] [timeMillis: 1652277298362] [levelValue: 1000] [[
  The java.lang.Object class was compiled with an unsupported JDK. Report this error to the EclipseLink open source project.
java.lang.ArrayIndexOutOfBoundsException: Index 8 out of bounds for length 0
	at org.eclipse.persistence.internal.libraries.asm.ClassReader.readUnsignedShort(ClassReader.java:3573)


[2022-05-11T09:54:58.364-0400] [] [WARNING] [] [org.eclipse.persistence.session./file:/path/to/payara-micro-plugin-group/payara-micro-plugin-example/target/payara-micro-plugin-example-1.0.0-SNAPSHOT/WEB-INF/classes/_MyAppPU.metadata] [tid: _ThreadID=1 _ThreadName=main] [timeMillis: 1652277298364] [levelValue: 900] [[
  The java.lang.String class was compiled with an unsupported JDK. Report this error to the EclipseLink open source project.
java.lang.IllegalArgumentException: Unsupported class file major version 62
	at org.eclipse.persistence.internal.libraries.asm.ClassReader.<init>(ClassReader.java:196)


[2022-05-11T09:54:58.372-0400] [] [WARNING] [] [org.eclipse.persistence.session./file:/path/to/payara-micro-plugin-group/payara-micro-plugin-example/target/payara-micro-plugin-example-1.0.0-SNAPSHOT/WEB-INF/classes/_MyAppPU.metadata] [tid: _ThreadID=1 _ThreadName=main] [timeMillis: 1652277298372] [levelValue: 900] [[
  The java.lang.Long class was compiled with an unsupported JDK. Report this error to the EclipseLink open source project.
java.lang.IllegalArgumentException: Unsupported class file major version 62
	at org.eclipse.persistence.internal.libraries.asm.ClassReader.<init>(ClassReader.java:196)


[2022-05-11T09:54:58.379-0400] [] [WARNING] [] [org.eclipse.persistence.session./file:/path/to/payara-micro-plugin-group/payara-micro-plugin-example/target/payara-micro-plugin-example-1.0.0-SNAPSHOT/WEB-INF/classes/_MyAppPU.metadata] [tid: _ThreadID=1 _ThreadName=main] [timeMillis: 1652277298379] [levelValue: 900] [[
  The java.lang.Number class was compiled with an unsupported JDK. Report this error to the EclipseLink open source project.
java.lang.IllegalArgumentException: Unsupported class file major version 62
	at org.eclipse.persistence.internal.libraries.asm.ClassReader.<init>(ClassReader.java:196)

The project is built with JDK 1.8, and maven is started with setting JAVA_HOME to JDK 1.8:

DATABASE_USER=user \
DATABASE_PASS=password \
DATABASE_NAME=MY_DB_NAME \
DATABASE_SERVER=localhost \
DATABASE_SERVER_PORT=1234 \
JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-amd64 \
M2_HOME=/usr/share/maven \
/usr/share/maven/bin/mvn payara-micro:start

Maven/JDK version:

$ JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-amd64 M2_HOME=/usr/share/maven /usr/share/maven/bin/mvn --version
Apache Maven 3.6.3
Maven home: /usr/share/maven
Java version: 1.8.0_312, vendor: Private Build, runtime: /usr/lib/jvm/java-8-openjdk-amd64/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "5.15.0-27-generic", arch: "amd64", family: "unix"

System Java (this shouldn’t be playing a factor):

$ java -version
openjdk version "18-ea" 2022-03-22
OpenJDK Runtime Environment (build 18-ea+36-Ubuntu-1)
OpenJDK 64-Bit Server VM (build 18-ea+36-Ubuntu-1, mixed mode, sharing)

The system default is Java 18, but I don’t see how that can even come into play. All included application dependencies were compiled with JDK 1.8 or earlier (I checked each one).

Why would it be picking up major version 62?

I found the cause. The plugin uses Apache Toolchain to locate java, and it’s finding the wrong one (ie, the system one).

One solution is to add the plugin <javaPath> option:

                <configuration>
                    <useUberJar>false</useUberJar>
                    <contextRoot>/myapp</contextRoot>
                    <payaraVersion>${version.payara}</payaraVersion>
                    <deployWar>false</deployWar>                    
                    <javaPath>/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/bin/java</javaPath>

Of course, that won’t work for devs on other systems like Windows of Mac.

The proper way is configure a ~/.m2/toolchains.xml file and set up Toolchain in the pom to use that.

~/.m2/toolchains.xml:

<?xml version="1.0" encoding="UTF-8"?>
<toolchains>
	<!-- JDK toolchains -->
	<toolchain>
		<type>jdk</type>
		<provides>
		  	<version>1.8</version>
		  	<vendor>openjdk</vendor>
		</provides>
		<configuration>
		  	<jdkHome>/usr/lib/jvm/java-1.8.0-openjdk-amd64</jdkHome>
		</configuration>
	</toolchain>
	<toolchain>
		<type>jdk</type>
		<provides>
		  	<version>11</version>
		  	<vendor>openjdk</vendor>
		</provides>
		<configuration>
		  	<jdkHome>/usr/lib/jvm/java-11-openjdk-amd64</jdkHome>
		</configuration>
	</toolchain>
	<toolchain>
		<type>jdk</type>
		<provides>
		  	<version>18</version>
		  	<vendor>openjdk</vendor>
		</provides>
		<configuration>
		  	<jdkHome>/usr/lib/jvm/java-18-openjdk-amd64</jdkHome>
		</configuration>
	</toolchain>
</toolchains>

pom.xml


            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.10.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-toolchains-plugin</artifactId>
                <version>1.1</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>toolchain</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <toolchains>
                        <jdk>
                            <version>1.8</version>
                            <vendor>openjdk</vendor>
                        </jdk>
                    </toolchains>
                </configuration>
            </plugin>
            <plugin>
                <groupId>fish.payara.maven.plugins</groupId>
                <artifactId>payara-micro-maven-plugin</artifactId>
                <version>1.4.0</version>
                <configuration>
                    <useUberJar>false</useUberJar>
                    <contextRoot>/myapp</contextRoot>
                    <payaraVersion>${version.payara}</payaraVersion>
                    <deployWar>false</deployWar>                    
                    <artifactItem>
                        <groupId>fish.payara.extras</groupId>
                        <artifactId>payara-micro</artifactId>
                        <version>${version.payara.micro}</version>
                    </artifactItem>
                    <javaCommandLineOptions>
                        <option>
                            <value>-Xdebug</value>
                        </option>
                    </javaCommandLineOptions>
                    <commandLineOptions>
                        <option>
                            <key>--nocluster</key>
                        </option>
                        <option>
                            <key>--port</key>
                            <value>8095</value>
                        </option>
                        <option>
                            <key>--postbootcommandfile</key>
                            <value>${project.basedir}/src/main/resources/post-boot-commands.txt</value>
                        </option>
                        <option>
                            <key>--deploy</key>
                            <value>${project.build.directory}/${project.build.finalName}</value>
                        </option>
                    </commandLineOptions>
                </configuration>
            </plugin>

Launch using nbactions.xml:

<?xml version="1.0" encoding="UTF-8"?>
<actions>
    <action>
        <actionName>CUSTOM-toolchains:toolchain payara-micro:start (with DB)</actionName>
        <displayName>toolchains:toolchain payara-micro:start (with DB)</displayName>
        <goals>
            <goal>toolchains:toolchain</goal>
            <goal>payara-micro:start</goal>
        </goals>
        <properties>
            <Env.DATABASE_USER>user</Env.DATABASE_USER>
            <Env.DATABASE_PASS>password</Env.DATABASE_PASS>
            <Env.DATABASE_NAME>DATABASE</Env.DATABASE_NAME>
            <Env.DATABASE_SERVER>localhost</Env.DATABASE_SERVER>
            <Env.DATABASE_SERVER_PORT>1234</Env.DATABASE_SERVER_PORT>
        </properties>
    </action>
</actions>

Launch using commandline:

DATABASE_USER=user \
DATABASE_PASS=password \
DATABASE_NAME=MY_DB_NAME \
DATABASE_SERVER=localhost \
DATABASE_SERVER_PORT=1234 \
JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-amd64 \
M2_HOME=/usr/share/maven \
/usr/share/maven/bin/mvn toolchains:toolchain payara-micro:start