1

I recently downloaded the JDK11 and the OpenJ9 JRE for JDK11. I have set the Java classpath fine.

When I run java -version command I get:

openjdk version "11.0.10" 2021-01-19
OpenJDK Runtime Environment AdoptOpenJDK (build 11.0.10+9)
Eclipse OpenJ9 VM AdoptOpenJDK (build openj9-0.24.0, JRE 11 Windows 10 amd64-64-Bit Compressed References 20210120_899 (JIT enabled, AOT enabled)
OpenJ9   - 345e1b09e
OMR      - 741e94ea8
JCL      - 0a86953833 based on jdk-11.0.10+9)

But my Maven is still pointing to the old Java8 that I have installed. Running mvn -version gives me:

Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
Maven home: H:\apache-maven-3.6.3\bin\..
Java version: 1.8.0_271, vendor: Oracle Corporation, runtime: C:\Program Files\Java\jdk1.8.0_271\jre
Default locale: en_US, platform encoding: Cp1252
OS name: "windows 10", version: "10.0", arch: "amd64", family: "windows"

Hence when I compile a maven module I get the following error:

Caused by: java.lang.IllegalArgumentException: invalid target release: 11

I have this set in my parent pom.xml:

<properties>

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

enter image description here

hell_storm2004
  • 1,401
  • 2
  • 33
  • 66
  • 1
    If you want to override java version for Maven, you need to set `JAVA_HOME` environment variable. – Ali Can Feb 24 '21 at 13:59
  • `java -version` gives you "the first from `${PATH}`", whereas maven (if you not hacked the built in scripts) relies heavily on `${JAVA_HOME}` environment variable... – xerx593 Feb 24 '21 at 14:12
  • My `JAVA_HOME` is set to `C:\Program Files\Java\jdk-11.0.10` – hell_storm2004 Feb 24 '21 at 14:21
  • Your output would seem to suggest otherwise. What does `echo %JAVA_HOME%` print out? – Gimby Feb 24 '21 at 14:26
  • Same as above. It prints the JDK 11 path. I will try to upload a screenshot to my original post. – hell_storm2004 Feb 24 '21 at 14:42
  • 2
    Okay, but the reason for this train of comments is because this is how it is documented to work and how it has worked for many years; it works like that for me right now. So then when I see a situation where the reality is deviating from how it is supposed to work, I can only assume a human has intervened somewhere. For example by manually editing the mvn.cmd file. – Gimby Feb 24 '21 at 15:27
  • 1
    yes, from your screenshot, this is the only possible conclusion: Someone modified the `mvn.cmd`! Please compare/replace yours with the [(3.6.3) release version](https://github.com/apache/maven/blob/maven-3.6.3/apache-maven/src/bin/mvn.cmd) – xerx593 Feb 26 '21 at 16:07
  • @Gimby You were right. My colleague sent me an updated cmd file long ago, which I copied, and that had the JDK hard coded. I am not sure of the validity of the question anymore! – hell_storm2004 Mar 03 '21 at 12:21

2 Answers2

2

You can use the element inside maven-compiler-plugin.

As described here How to tell compiler to use openjdk 11 via pom.xml Here is a bit more advanced example that allows you to compile Java 11 while your main java version is 8. Most of my projects are still java 8 so I find it usefull.

<plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.0</version>
            <configuration>
                <release>11</release>
                <fork>true</fork>
                <executable>C:\Program Files\Java\jdk-11.0.2\bin\javac</executable>
            </configuration>
        </plugin>
Alexander Petrov
  • 9,204
  • 31
  • 70
0

I use the toolchain mechanism of maven, works fine, you can switch easily of jdk compiler regarding which the project :

    <toolchains xmlns="http://maven.apache.org/TOOLCHAINS/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xsi:schemaLocation="http://maven.apache.org/TOOLCHAINS/1.1.0 http://maven.apache.org/xsd/toolchains-1.1.0.xsd">
      <toolchain>
        <type>jdk</type>
        <provides>
          <version>1.8</version>
        </provides>
        <configuration>
          <jdkHome>your_java_8_local_path</jdkHome>
        </configuration>
      </toolchain>
      <toolchain>
        <type>jdk</type>
        <provides>
          <version>11</version>
        </provides>
        <configuration>
          <jdkHome>your_java_11_local_path</jdkHome>      
        </configuration>
      </toolchain>
    </toolchains>

Then just declare it in your pom.xml like this (compiling with jdk 11 by example here) :

<build>     
    <pluginManagement>
        <plugins>
            <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>11</version>
                        </jdk>
                    </toolchains>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-toolchains-plugin</artifactId>
            </plugin>
     </plugins>
</build>    
jcamsler
  • 61
  • 3