Compile using -source and -target javac options

11,719

Solution 1

Compiler configuration without plugin

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

javac -help

javac -help

  -source <release>          Provide source compatibility with specified release
  -target <release>          Generate class files for specific VM version

javac - Java programming language compiler documentation

source Specifies the version of source code accepted. The following values for release are allowed:

  • 1.6 No language changes were introduced in Java SE 6. However, encoding errors in source files are now reported as errors instead of warnings as in previous releases of Java SE.
  • 6 Synonym for 1.6.
  • 1.7 This is the default value. The compiler accepts code with features introduced in Java SE 7.
  • 7 Synonym for 1.7.

target Generate class files that target a specified version of the VM. Class files will run on the specified target and on later versions, but not on earlier versions of the VM. Valid targets are 1.1, 1.2, 1.3, 1.4, 1.5 (also 5), 1.6 (also 6), and 1.7 (also 7). The default for -target depends on the value of -source:

Compatibility

Solution 2

With the settings you have, the source code is interpreted as Java 1.6 compliant and the generated classes will target 1.6 JVM. JDK 1.7 can deal with it. But if your code is 1.7 and want to target only 1.7 then you should write 1.7 in both. Note that if your source code includes features that are only in 1.7 available, diamond operator for instance, then you'll get compile error.

With the settings

<source>1.6</source>
<target>1.7</target>

the following 1.7 code (diamond operator)

List<String> l = new ArrayList<>();

will fail to compile with the following error:

... Compilation failure
... App.java:[14,40] diamond operator is not supported in -source 1.6
(use -source 7 or higher to enable diamond operator)

Whereas the following code will compile just fine

List<String> l = new ArrayList<String>();

with the settings:

<source>1.7</source>
<target>1.6</target>

The following 1.7 code (diamond operator)

List<String> l = new ArrayList<>();

will also fail to compile with the following error:

source release 1.7 requires target release 1.7

So if you want to use source features of 1.7 (diamond operator, try-with-resources, etc.) then you have to have the following in your pom.xml.

<source>1.7</source>
<target>1.7</target>
Share:
11,719
M Sach
Author by

M Sach

Updated on June 04, 2022

Comments

  • M Sach
    M Sach almost 2 years

    I have upgraded my web application to Java 7 with JAVA_HOME pointing to 1.7. My Maven plugin is reading the Java version from java_home. But I am bit confused after seeing the below setting in pom.xml:

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.3.2</version>
        <configuration>
            <fork>true</fork>
            <compilerVersion>1.6</compilerVersion>
            <source>1.6</source>
            <target>1.6</target>
        </configuration>
    </plugin>
    

    If I keep the above settings as it is, will Maven compile the Java code with 1.6 or 1.7? As per my understanding based on this link, the above settings will get preference and the project will be compiled with 1.6 instead of 1.7. Is that correct?

    If I give a setting like below and if I have code specific to JDK 1.7, will my code compile now?

     <source>1.6</source>
      <target>1.7</target>
    

    I am not sure; what do the above settings actually mean?

  • M Sach
    M Sach over 10 years
    please see my update and let me know your take on this
  • A4L
    A4L over 10 years
    @MSach please check my edit.