exec-maven-plugin Java version different from maven-compiler-plugin Java version in NetBeans project

Viewed 480

I start up the NetBeans IDE 12.0 with a system environment JAVA_HOME (1.8) lower than what the IDE uses (14). The IDE output window of Maven execution seems to show that the project is run with JAVA_HOME for JDK 14, running project files this way.

But the exec-maven-plugin fails with java.lang.UnsupportedClassVersionError: com/mycompany/Start has been compiled by a more recent version of the Java Runtime (class file version 55.0), this version of the Java Runtime only recognizes class file versions up to 52.0

This seems to indicate that Maven runs under 1.8 in the IDE, ignoring the IDE's own override setting of JAVA_HOME of 14. It uses the JDK as set in JAVA_HOME, because if I set JAVA_HOME to 14.0 before IDE startup then it works.

I am looking for the correct way, the right places, to set the JDK for any project specifically, regardless of the JAVA_HOME setting that is active when the IDE starts.

My case can be reproduced by running the IDE 12.0 with JDK 14.0 installed, but with JAVA_HOME set to 1.8. before IDE startup. I use the Maven project generated from the command at https://wicket.apache.org/start/quickstart.html - all set to defaults. The file to run is Start.java.

This is important to me because I must open many different projects with different JDKs so it is not practical to re-start the IDE with different JAVA_HOME settings each time. As it is, it appears that my setting is poorly defined - I must be missing something.

I should add that in the project properties, I have set the Build|Compile|Java Platform to JDK 14 (Default) as expected.

I have filed a NetBeans Issue

1 Answers

For maven, if you want the byte code of your project to be 1.8, try just setting variables used by the compiler plugin even if you're using a newer JDK.

<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>

https://maven.apache.org/plugins/maven-compiler-plugin/examples/set-compiler-source-and-target.html

If you want a different java install completely, then you might want to look into the toolchains. But I'd only go this route if the above doesn't meet your needs. https://maven.apache.org/guides/mini/guide-using-toolchains.html

I am not familiar with NetBeans but hopefully it will read the compiler level for maven.

Related