2

I want to run scala compilation with specific JVM. I'm using Scalatra, and my default JVM is 1.8. With such setup, I run into this problem: Compilation failed: error while loading AnnotatedElement, ConcurrentMap, CharSequence from Java 8 under Scala 2.10?

So I want to use JVM 1.7 for this, how to set it up for gradle "scala" plugin?

Community
  • 1
  • 1
amorfis
  • 15,390
  • 15
  • 77
  • 125

2 Answers2

2

How to instruct gradle's scala plugin to pick specific JVM among installed ones?

This isn't currently supported. The best solution is to run Gradle itself with the desired JVM. It might be possible to instead set a different JVM bootstrap class path via...

tasks.withType(ScalaCompile) {
    scalaCompileOptions.with {
        useAnt = false
        fork = true
        forkOptions.jvmArgs = [...]
    }
}

..., but you would have to figure out the details yourself (see JVM docs). Also you would have to know the JVM installation path.

Peter Niederwieser
  • 121,412
  • 21
  • 324
  • 259
0

The scalac compiler has special parameters

–javabootclasspath path
Override Java boot classpath.
–javaextdirs path
Override Java extdirs classpath.

You have to pass those parameters from gradle scala plugin using an additionalParameters parameter

this approach does not requires forking

allprojects {
  tasks.withType(ScalaCompile) {
    if (sourceCompatibility == '1.7') {
        scalaCompileOptions.with {
            def jdk7rt = new File("$System.env.JAVA7_HOME", "jre/lib/rt.jar").canonicalPath
            def jdk7ext = new File("$System.env.JAVA7_HOME", "jre/lib/ext").canonicalPath
            additionalParameters = ["-javabootclasspath $jdk7rt".toString(), "-javaextdirs $jdk7ext".toString()]
        }

    }
  }
}

I used this approach on my OS with JVM 1.7 as default for compiling with JDK8 some subprojects that requires javafx8. So it should work in your case.