0

I have JDK 1.8 installed and configured JAVA_HOME to JDK 1.8. I am using apache-maven-3.0.5. I have created a maven project and configured maven-compiler-plugin(3.3) <source> and <target> to 1.7.

The problem is if I use any Java 1.8 features like String.join() or map.remove(key, value) it is still compiling successfully even though I set compiler level to 1.7.

One thing I observed is if I point my JAVA_HOME to JDK 1.7 directory then it is throwing compilation errors.

But my expectation is even if we have JAVA_HOME pointing to JDK 1.8 when I set compiler level to 1.7 in my pom.xml it should obey that compiler level(1.7) and throw errors.

What am I missing?

PS: When I run from my Eclipse IDE it is using correct JDK(1.7) and giving errors. But when I run it from command-line i am running into this problem.

K. Siva Prasad Reddy
  • 11,786
  • 12
  • 68
  • 95

2 Answers2

3

You are mixing two things:

  • compiler (e.g. default methods, lambdas)
  • java libraries/API (e.g. Stream, Optional, String.join)

Maven can check only compiler level, what you are doing is using libraries that are in JDK 1.8, which is OK, unless you want the code to run on JDK 1.7.

Moreover even java doesn't always know which methods/classes appeared in newer versions of standard library (not all are marked with @since tag) so if you would try to do compliation with javac you would get similar results.

Krzysztof Krasoń
  • 26,515
  • 16
  • 89
  • 115
2

Changing the source and target levels will not check that you are using 1.8 API when compiling with JDK 8. It will only check language features.

You can use the Animal Sniffer Maven plugin and do an API check, which can be set up to fail the build if you try to use 1.8 API.

prunge
  • 22,460
  • 3
  • 73
  • 80