0

Maven 3, Windows 7, JDK1.7.0_51

I have the following very simple Maven POM

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.gwl</groupId>
  <artifactId>test-lineseparator</artifactId>
  <version>0.0.1-SNAPSHOT</version>
</project>

and the following very simple test class

public class TestLineSeparator {
    public static void main(String[] args) {
        System.out.print("This is a line using System.lineSeparator()." + System.lineSeparator());
    }
}
  • I have jdk1.7.0_51 installed as my default build environment.
  • The default src and target args for the Maven compiler plugin are 1.5.
  • The System.lineSeparator() method was not introduced until 1.7
  • Maven compiles the code without issue. It will run OK under a 1.7 JRE/JDK, but 1.6 or earlier complains

    Exception in thread "main" java.lang.NoSuchMethodError: java.lang.System.lineSeparator()Ljava/lang/String;

Q. Why does Maven not complain about System.lineSeparator() not existing when its compiler src argument defaults to 1.5?

(Note, the same question applies to an Eclipse projected generated from the same POM. Despite 1.5 Compiler Compliance and a 1.5 Execution Environment being set, there are no complaints)

Gordon Little
  • 141
  • 1
  • 2
  • 16

1 Answers1

1

compilation as 1.5 means java code structure / class file design to be at level 1.5 - not to be confused with jdk code itself

If jdk 1.7 ships a rt.jar file with System.java code with some methods, javac compiler has no way to know what it was as of 1.6, 1.5, 1.4 level (unless you have classes.zip and specify the boot classpath)

see javac source and target options

http://www.javaworld.com/article/2077388/core-java/what-version-is-your-java-code.html

To make life easy use the right jdk for the right job

Community
  • 1
  • 1
Kalpesh Soni
  • 6,879
  • 2
  • 56
  • 59
  • 1
    "To make life easy use the right jdk for the right job" The same conclusion I'd come to but good explanation, thanks! – Gordon Little Oct 07 '14 at 15:19