9

I am trying to build a maven project on centOS 8. I want maven to use Java version 15. When I run mvn package I get the following error:

Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile 
(default-compile) on project systembrett-backend: Fatal error compiling: invalid target release: 15 -> [Help 1]

So I suspect that maven is using the wrong Java version, because when i do mvn package -X for debug logs it start with:

Apache Maven 3.5.4 (Red Hat 3.5.4-5)
Maven home: /usr/share/maven
Java version: 1.8.0_275, vendor: Red Hat, Inc., runtime: /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.275.b01-1.el8_3.x86_64/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "4.18.0-240.1.1.el8_3.x86_64", arch: "amd64", family: "unix"

so it looks like, maven is using Java version 1.8. But mvn -version says:

Apache Maven 3.5.4 (Red Hat 3.5.4-5)
Maven home: /usr/share/maven
Java version: 15.0.2, vendor: AdoptOpenJDK, runtime: /opt/jdk-15.0.2+7
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "4.18.0-240.1.1.el8_3.x86_64", arch: "amd64", family: "unix"

JAVA_HOME is /opt/jdk-15.0.2+7 and PATH is /opt/jdk-15.0.2+7/bin:/home/username/.local/bin:/home/username/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin.

I thought maven is choosing the java version by checking JAVA_HOME, but apparently it is using an other version for the build. Does anyone know how to tell maven the correct version? Thanks!

1

3 Answers 3

8

Following is a list of steps I use to troubleshoot this kind of issues:

  1. Linux usually works with alternatives to ensure proper default Java environment is used. Similar to:
$ alternatives --config java

There are 2 programs which provide 'java'.

  Selection    Command
-----------------------------------------------
 + 1           java-1.7.0-openjdk.x86_64 (/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.191-2.6.15.4.el7_5.x86_64/jre/bin/java)
*  2           java-1.8.0-openjdk.x86_64 (/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.181-3.b13.el7_5.x86_64/jre/bin/java)

Try also for javac as it may not be configured the same way:

$ alternatives --config javac
  1. For your maven instance, JAVA_HOME should be enough.

Your output of mvn -version testifies that you have configured it correctly. Remove your Java from your PATH to ensure JAVA_HOME and mvn will find the correct one.

  1. The pom.xml can also configure the required compiler, similar to:
<properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
</properties>
  1. Another thing may be that you're not using the same ENV (different terminals may not have the same environment variables exported - I presume you being on CentOS, you already encountered this). You have to exit the terminal and get back in to allow the default variables to take effect.

You encounter this usually when different JRE vs JDK are used ( more: maven installation has runtime as JRE instead of JDK )

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! Is there any way to automate alternatives selection when we install a new JDK?
Each distro will have different conventions. The distro package (e.g. .rpm, .deb etc) is responsible for managing the alternatives. The alternatives can be configured at the user level too, using a Configuration Management framework or manually. Feel free to raise another stackoverflow question with the exact need, and we can work on that.
4

I had the same issue on RHEL 9.1. The solution was to look at /etc/java/maven.conf and set the correct JAVA_HOME here in this file.

In other words, you can add just the single line similar to this to that maven.conf file:

JAVA_HOME=/usr/lib/jvm/java-17-openjdk

and you should be good.

2 Comments

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
I edited my /etc/java/maven.conf to use /usr/lib/jvm/jre so that it can work when changing java version with alternatives --config java. If the path was instead set to /usr/lib/jvm/jre-openjdk then it would need to be updated with alternatives --config java_sdk_openjdk
2

Additionally from checking environment variable JAVA_HOME, file .m2/settings.xml on your home directory, and files /etc/java/maven.conf and $MAVEN_HOME/conf/maven.conf for JVM paths, there is a pretty obscure way to configure maven JVM user-wide.

You can define a file .mavenrc on your home directory Just put JAVA_HOME=<jdk path as in JAVA_HOME> on .mavenrc and maven will automatically use that JDK.

If your maven is reverting to some wrong JVM you can check if ~/.mavenrc exists and if the unwanted JVM path is defined there.

1 Comment

this setting worked for me, thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.