Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sourceCompatibility does not work as expected #23675

Closed
jie-huang opened this issue Jan 26, 2023 · 1 comment
Closed

sourceCompatibility does not work as expected #23675

jie-huang opened this issue Jan 26, 2023 · 1 comment
Labels
closed:question usage question or request for help

Comments

@jie-huang
Copy link

Expected Behavior

java {
  sourceCompatibility = JavaVersion.VERSION_11
}

It should use only Java 11 features to compile.

Current Behavior

When using new features, e.g. a Java API in new version, it does not report error.

Context

When build a java lib, using sourceCompatibility does not detect the version compatibility problem.

Steps to Reproduce

# build.gradle file
plugins {
    id 'java-library'
}

group = 'jie'
version = '1.0.0'

dependencies {
}

java {
//    toolchain {
//        languageVersion = JavaLanguageVersion.of(11)
//    }

    sourceCompatibility = JavaVersion.VERSION_11
    targetCompatibility = JavaVersion.VERSION_11
}
// Java file to test
import java.util.Random;

public class Hello {
  public long getRandom() {
    Random r = new Random();
   // nextLong(long, long) is available in Java 17
    return r.nextLong(100L, 500L);
  }
}
  1. run ./gradlew build, it passed
  2. comment out sourceCompatibility, and use toolchain config, run ./gradlew build again, it failed as expected because the api version
java {
    toolchain {
        languageVersion = JavaLanguageVersion.of(11)
    }
}

result: error: method nextLong in class Random cannot be applied to given types;

Your Environment

$ ./gradlew --version

------------------------------------------------------------
Gradle 7.6
------------------------------------------------------------

Build time:   2022-11-25 13:35:10 UTC
Revision:     daece9dbc5b79370cc8e4fd6fe4b2cd400e150a8

Kotlin:       1.7.10
Groovy:       3.0.13
Ant:          Apache Ant(TM) version 1.10.11 compiled on July 10 2021
JVM:          18 (Oracle Corporation 18+36-2087)
OS:           Mac OS X 12.6.3 aarch64
$ java --version
openjdk 18 2022-03-22
OpenJDK Runtime Environment (build 18+36-2087)
OpenJDK 64-Bit Server VM (build 18+36-2087, mixed mode, sharing)
@eskatos eskatos added closed:question usage question or request for help and removed a:bug to-triage labels Feb 1, 2023
@eskatos
Copy link
Member

eskatos commented Feb 1, 2023

Thank you for your interest in Gradle!

This is the intended behavior and there’s no plan to change it. This issue will be closed.


What you are looking for is the release option:
https://docs.oracle.com/en/java/javase/17/docs/specs/man/javac.html#option-release

@eskatos eskatos closed this as not planned Won't fix, can't repro, duplicate, stale Feb 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed:question usage question or request for help
Projects
None yet
Development

No branches or pull requests

2 participants