Compile with a different JDK fails in a mixed Java and Groovy environment

// Compile with a different JDK fails in a mixedJavaAndGroovy environment // Example: D:\Buildoptimierung\gradle-1.4\samples\groovy\mixedJavaAndGroovy // JAVA_HOME=C:\Program Files\Java\jdk1.6.0_26 // Gradle 1.4 // Groovy 1.8.6

apply plugin: ‘groovy’ version = 1.0

repositories { }

dependencies {

compile localGroovy() }

compileGroovy{

// ERROR: jdk1.6.0_26 is used instead of jdk1.6.0_35

options.fork = false // If true, you get an “Unexpeceted exception thrown”.

options.forkOptions.executable = “C:/Program Files/Java/jdk1.6.0_35/bin/javac.exe”

options.forkOptions.jvmArgs = [’-verbose:class’] } compileJava{

options.fork = true

options.forkOptions.executable = “C:/Program Files/Java/jdk1.6.0_35/bin/javac.exe”

//options.forkOptions.jvmArgs = [’-J-verbose:class’] }

Fork options only take effect with ‘options.fork = true’. Would you mind to provide the full error message and stack trace (’-S’)? Do you have Java code that needs to get joint-compiled, or is it good enough to compile Java and Groovy code separately?

For long code snippets, stack traces or debug logs please share via a [pastebin service](pastebin service) (e.g. [GitHub Gist](GitHub Gist)).

We have Java and Groovy code separatly.

If you don’t use joint compilation (i.e. don’t have Java code under ‘src/main/groovy’), then you shouldn’t have a problem. How do you tell that the wrong compiler is used?

Sorry, I don’t knew what you meant by joint compilation.

In the example from gradle “\gradle-1.4\samples\groovy\mixedJavaAndGroovy\src\main” you have

src/main/groovy and src/main/java as we have in our application.

Here is a simpler example:

This code doesn’t work and that is our problem. It throws the Exception I posted: // Compile with a different JDK fails in a mixedJavaAndGroovy environment // Example: D:\Buildoptimierung\gradle-1.4\samples\groovy\mixedJavaAndGroovy // JAVA_HOME=C:\Program Files\Java\jdk1.6.0_26 // Gradle 1.4 // Groovy 1.8.6

apply plugin: ‘groovy’ version = 1.0

repositories { }

dependencies {

compile localGroovy() }

compileGroovy{

options.fork = true

options.forkOptions.executable = “C:/Program Files/Java/jdk1.6.0_35/bin/javac.exe” }

‘compileGroovy.options’ are the Java compile options for Groovy/Java joint compilation. If you compile Groovy and Java separately, you’ll need to configure ‘compileJava.options’ instead.

Maybe we don’t talk about the same thing. If I set the compileJava.options the groovy sources are still compiled with jdk1.6.0_26 and the java sources with jdk1.6.0_35. If I set the compileGroovy.options additionally an exception is thrown. So what should I do to compile groovy and java sources with jdk1.6.0_35 when JAVA_HOME is set to jdk1.6.0_26. I will run one gradle script and I don’t want to change JAVA_HOME. I do that on a

WINDOWS plattform and I use an example of gradle as you can see.

With the statement “options.forkOptions.jvmArgs = [’-verbose:class’]” you can control what jdk is used for compilation.

D:\Buildoptimierung\gradle-1.4\samples\groovy\mixedJavaAndGroovy>gradle clean assemble :clean :compileJava :compileGroovy :processResources UP-TO-DATE :classes :jar :assemble

BUILD SUCCESSFUL

In your code snippets, you are always setting the ‘javac’ executable. However, Groovy has its own compiler and doesn’t use ‘javac’. Hence I thought you were trying to set the Java compiler to be used, possibly in Groovy/Java joint compilation. What’s your real goal here?

Our problem is, that we can not use

compileGroovy{
                 options.fork = true
                 options.forkOptions.executable = "C:/Program Files/Java/jdk1.6.0_35/bin/javac.exe"
 }

because of the posted exception.

You can reproduce the error with

gradle-1.4\samples\groovy\mixedJavaAndGroovy

by adding the compileGroovy section to the build.gradle of the sample.

We want to use Groovy/Java joint compilation. Using JAVA_HOME to choose the jdk for compiling *.java is not an option.

Our problem is, that we can not use >

compileGroovy{

options.fork = true

options.forkOptions.executable = “C:/Program Files/Java/jdk1.6.0_35/bin/javac.exe”

} > because of the posted exception.

The problem, as Peter explained it, is that you’re trying to use ‘javac’ to compile groovy code. This is simply not possible. The ‘javac’ compiler is for java code.

To use Groovy/Java joint compilation, you need to use the Groovy compiler, which is able to compile both Groovy and Java source files.