Compile Java 1.6 compatible classes with gradle started with java 7

I’ve always multiple Jdk versions on my machine. So usually when I start gradle it would use the newest one available on my machine. But some projects I have to build are 1.5 or 1.6 projects.

Is there a way to tell gradle which specific jdk is to be used to compile a certain project, so the source-, target- and classpath compatibility would be correct set?

Using sourceCompatibility, targetCompatibility properties seems to be not enough because the source and target compatibliliy from the class file point of view is ok but I get always new classes (from jdk7) referenced by accident which break compatibility. Especially when I use gradle eclipse plugin which links with the new class lib (jdk7) so I get new code completion proposals.

So i thought property would fix it. But than I get the following warning issued by the compiler:

It is recommended that the compiler be upgraded.
warning: java\sql\DriverManager.class(java\sql:DriverManager.class): major version 51 is newer than 50, the highest major version supported
by this compiler.

Actually I think I need to be able to set the -bootclasspath compiler option somehow. Thanks, david

Got it:

compileJava {
  sourceCompatibility = 1.6
  targetCompatibility = 1.6
  options.bootClasspath = "${System.getProperty('java6.home')}/jre/lib/rt.jar"
1 Like

Your solution is fine. You should get the same experience by setting ‘’ instead of ‘compileJava.options.bootClasspath’, except that this would also cause the build itself to run with a 1.6 JVM. Possibly you’ve been affected by GRADLE-2460, which has been fixed in master and will ship with 1.3.