Search code examples
javamaven

Compiling for an earlier version of JRE using language constructs from later JDK versions


I have a question about compiling a programme for an earlier version of JRE. I have JDK 21 installed on my work computer (and it is also connected in Eclipse).

Case 1. There is a common Eclipse project with the following setting: Compiler compliance level -> 1.8 And the programme code

public class Test21Main {
    public static void main(String[] args) {
        List<String> fixedSizeList = Arrays.asList(‘Male’, ‘Female’);
        String last = fixedSizeList.getLast();
        System.out.println(last);
    }
}

The IDE shows an error in the editor on the method getLast(); - The method getLast() is undefined for the type List<String> (because this method appeared only in JDK21). Accordingly, this project cannot be compiled because there is a Unresolved compilation problem: The method getLast() is undefined for the type List<String>

Case 2. There is a Maven project, in which in pom.xml is specified

<properties>
<project.java.version>1.8</project.java.version>
</properties>
...
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.7.0</version>
    <configuration>
        <source>${project.java.version}</source>
        <target>${project.java.version}</target>                
    </configuration>
</plugin>

I.e. this maven project will compile for jre 1.8 and moreover even the sources are specified as 1.8.

The getLast() method is also used in the code of this maven project, but it does not show any error in the IDE. This maven project compiles for jre 1.8, even though it uses the language construct for jdk 21.

So, it is not clear why the maven project does not show errors where the normal project in ide (Eclise) does? How the maven project compiles in an early version of jre (1.8), even though the source code uses a construct from jdk 21? I'm already repeating myself here, but only to describe the issues that cause misunderstandings.


Solution

  • You can compile this, but it will not run under a JDK 8. For instance, it will fail with: Exception in thread "main" java.lang.NoSuchMethodError: java.util.List.getLast()Ljava/lang/Object;

    To make sure that you compile something that will actually run under JDK 8, you need to set the property <maven.compiler.release> as well, or alternatively the <release> in the plugin configuration.

    <properties>
        <maven.compiler.release>8</maven.compiler.release>
    </properties>
    

    If the code does not satisfy the parameter rule above, a compilation error will occur, e.g. COMPILATION ERROR : cannot find symbol method getLast()

    Setting the --release of the Java Compiler