2

I have JDK 7 and 8 installed in my PC.

I try to set JAVA_HOME to JDK 8 and in the maven pom file, I set to 1.7 as below:

<properties>
    <maven.compiler.target>1.7</maven.compiler.target>
    <maven.compiler.source>1.7</maven.compiler.source>
</properties>

I got the error below during maven build:

incomparable types: boolean and java.lang.Object

The source code is:

Map mapData = (LinkedHashMap)it.next();
if(true == mapData.get("isTrueOrFalse")){ // java 8 doesn't allow this, it have to be [true == (boolean)mapData.get("isTrueOrFalse")]
    xxx
}

I can't change the source code, so I change my JAVA_HOME to JDK 7 and maven pom remain as 1.7. Then I can successfully build via Maven.

My understanding is, by setting the source and target, it should allow me to compile onto lower compatible Java version, but it is not. Can anyone help to explain this?

Sam YC
  • 10,725
  • 19
  • 102
  • 158

2 Answers2

0

Apache Maven page says that:

Merely setting the target option does not guarantee that your code actually runs on a JRE with the specified version (...) In the same way, setting the source option does not guarantee that your code actually compiles on a JDK with the specified version

You could try first configuring the plugin directly (instead of what you have on pom.xml):

<plugins>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.8.1</version>
    <configuration>
      <verbose>true</verbose>
      <fork>true</fork>
      <executable><!-- path-to-javac --></executable>
      <compilerVersion>1.7</compilerVersion>
      <source>1.7</source>
      <target>1.7</target>
    </configuration>
  </plugin>
</plugins>

Lastly, you can try Compiling Sources Using A Different JDK

Villat
  • 1,455
  • 1
  • 16
  • 33
  • I am not merely setting `target` only, I set for both `source` and `target` to same value. Tried to set plugin directly, it is same issue too. Don't think `Compiling Sources Using A Different JDK` will work, because the fundamental problem is at the `javac`, changing it to another JDK, it doesn't explain why I can't compile using JDK 8 with `-source 1.7`. – Sam YC Oct 11 '19 at 04:11
  • @GMsoF I've edited my answer, please try to use that configuration that forks the compiler an sets the executable to your JDK 1.7 path (change that line according to your computer path). – Villat Oct 11 '19 at 04:18
  • @GMsoF it must be the full path to javac, like YOUR_JAVA_HOME/bin/javac – Villat Oct 11 '19 at 04:20
  • If I change the path to JDK 1.7, then it works as my example showed, it is same as setting it from the `JAVA_HOME`. My question is more on, why if I use JDK 1.8 and setting the compilation level to 1.7, it doesn't work. By reading from Oracle doc, `javac` should work as expected https://docs.oracle.com/en/java/javase/11/tools/javac.html#GUID-AEEC9F07-CB49-4E96-8BC7-BCC2C7F725C9. Underneath, Maven is using `javac` also. – Sam YC Oct 11 '19 at 04:29
  • The `source` option doesn't instruct `javac` to compile the source files with the specified JDK version, it instructs `javac` to check the version of the accepted source code. On the other hand, the `target` option ensures that the generated class files will be compatible with that version. – Villat Oct 11 '19 at 04:35
  • @GMsoF notice that you sent me the Java 11 documentation, which is not the same as Java 8. – Villat Oct 11 '19 at 04:40
0

After so many of searching of Java compatibility post, I found two possible reasons why this is happening:

1) this is a bug in JDK 7, it should not allow JDK 7 to compile this as the type is not match. This is fixed in JDK 8, so even we use the -source=1.7 and -target=1.7, it is not allowed to go through. JDK 1.7 breaks backward compatibility? (generics)

2) this might due to the Java implementation return type not compatible, while using JDK 8 compile to -source=1.7 and -target=1.7, the build path (bootstrap classes) will be still pointing to JDK 8, as so the implementation of Java Map may return different type which cause issue above. Issue about java 8 backward compatibility: new methods in JDK

Sam YC
  • 10,725
  • 19
  • 102
  • 158