Scala compiler is unable to find a javac compiler under Mac OS with Java 1.7, gradle 1.1

Hello, I’m currently running into a strange problem with Java 1.7 under Mac OS.

I have set the JDK 1.7 path in my gradle.properties file like this:

org.gradle.daemon=true

org.gradle.jvmargs=-Xmx1g -XX:MaxPermSize=128m

org.gradle.java.home=/Library/Java/JavaVirtualMachines/jdk1.7.0_07.jdk/Contents/Home

When I run a full build for my scala project, I get

FAILURE: Build failed with an exception.

  • What went wrong:

Execution failed for task ‘:compileScala’.

Unable to find a javac compiler;

com.sun.tools.javac.Main is not on the classpath.

Perhaps JAVA_HOME does not point to the JDK.

It is currently set to “/Library/Java/JavaVirtualMachines/jdk1.7.0_07.jdk/Contents/Home/jre”

  • Try:

Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

(Note aside: this happens because there are also java sources in the scala source path. If there are only scala source files, everything is fine)

So the question is: why is gradle using the JRE and not the JDK path (since gradle.properties points to the JDK)

I have no JAVA_HOME set. The java command on the path, is a java 1.6.

However, after setting

export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.7.0_07.jdk/Contents/Home

the problem is gone.

Nevertheless I would expect that providing

org.gradle.java.home=/Library/Java/JavaVirtualMachines/jdk1.7.0_07.jdk/Contents/Home

should be enough.

Cheers, Oliver

In all likelihood, Gradle is using the JDK (you can check with ‘println System.getProperty(“java.version”)’ in the build script). It’s the Scala compiler that doesn’t find ‘javac’. Apparently the Scala compiler expects the ‘JAVA_HOME’ environment variable to be set, and apparently setting ‘org.gradle.java.home’ doesn’t also set the environment variable. Maybe it should.

The other question is whether the Scala compiler supports the official JDK6 compiler API (instead of the proprietary JDK5 ‘com.sun.tools.javac.Main’), which it should find on the class path. From the error message, it sounds like it doesn’t support it (and it definitely should). It might be worthwhile to raise this on the Scala list.

What does this print?

task debug << {
  println System.getProperty("java.home")
}

Without JAVA_HOME and org.gradle.java.home set it prints

/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home

which is ok and everything works fine.

Having org.gradle.java.home set to /Library/Java/JavaVirtualMachines/jdk1.7.0_07.jdk/Contents/Home and still no JAVA_HOME the output is

/Library/Java/JavaVirtualMachines/jdk1.7.0_07.jdk/Contents/Home/jre

which does not work (because of the jre).

Setting JAVA_HOME to the same path as org.gradle.java.home doesn’t change the output (still …/Home/jre), however, now the scala compiler is able to find javac.

I think our JDK detection logic goes wrong in that case because it’s a Mac but nevertheless has the Oracle JDK directory layout rather than the Apple JDK layout. I’ve raised GRADLE-2460 for this.

It looks like the Scala compiler falls back to the ‘java.home’ system property in case the ‘JAVA_HOME’ environment variable is absent. However, ‘java.home’ always points to a JRE rather than a JDK (this is the expected behavior). What I don’t understand is why the Scala compiler doesn’t find ‘com.sun.tools.javac.Main’ on the class path.

That said, I’m not able to reproduce your problem (I’ve tried with ‘samples/scala/mixedJavaAndScala’ from the full Gradle distribution). Can you provide a reproducible example? Also, which Scala version are you on?

Probably not the smallest, but for the moment the quickest self-containing example: Download https://github.com/b3ku/rucksac and run gradle. (And don’t forget to provide a ‘org.gradle.java.home’ in your own ‘~/.gradle/gradle.properties’) We’re using Scala 2.9.2.

Thanks

I’ve just detected that I had set an environment variable JAVA_VERSION=1.6

When I unset this variable or alternatively set it to 1.7 everything works fine.

Only with the value 1.6 the problem above occurs.

Unsure what this means as I can reproduce the problem without having JAVA_VERSION set.