0

I have a scala based project built with gradle, it contains java and scala code. We are upgrading to OpenJDK11 from oracle jdk 8. We are upgrading with binaries compatible with JDK8 by adding release flag to compiler. I achieved that for java files by

if(JavaVersion.current() > JavaVersion.VERSION_1_9) {
    subprojects {
        tasks.withType(JavaCompile) {
            options.compilerArgs += ["--release", "8"]
        }
        tasks.withType(ScalaCompile) {
            options.compilerArgs += ["--release", "8"]
        }
    }
}

in build.gradle. This works fine ie the build seems to add the release flag, to test this I added a JDK 11 specific change to java class. ie Used String.repeat in java class and compiler throws error but if I add the same to a scala class it compiles without reporting the error that repeat() is not an method available. What am I missing does adding release flag to scala compilation mean nothing or how can I achieve this? The intention is to have binaries that run on JDK 8 or JDK11 we are ok for now to compromise on using JDK11 based new features. So I need to make sure no other developers add jdk11 specific code so that we end up on runtime issues.

sinanspd
  • 2,589
  • 3
  • 19
  • 37
Annapoorni D
  • 831
  • 2
  • 13
  • 30
  • The Scala option has only one hyphen so `-release` also it would be good to also add the `-target:8` flag. – Luis Miguel Mejía Suárez Jan 04 '21 at 15:03
  • -release throws error, It accepts --release if given as above script. Many other searches led to adding source and target compatibility. But question is will that prevent users from using API's from jdk11. From what I tried it allows to compile and build but throws error on runtime – Annapoorni D Jan 05 '21 at 09:26
  • It should work with one hyphen, see [this](https://docs.scala-lang.org/overviews/compiler-options/index.html), so it seems that maybe those options are only applied to the **Java** compiler instead to the **Scala** compiler? That could explain why it didn't work. - `release` determines which **Java** stdlib to use, `target` determines which version of byte code to generate. – Luis Miguel Mejía Suárez Jan 05 '21 at 13:05

0 Answers0