Can't build Java 1.8 even with proper Java version in use

Hi, my Java Home is set properly and I still can’t get Java 1.8 to build.

~/myproject $ java -version java version “1.8.0_20” Java™ SE Runtime Environment (build 1.8.0_20-b26) Java HotSpot™ 64-Bit Server VM (build 25.20-b23, mixed mode)

~/myproject $ gradle clean build :clean :compileJava FAILED

FAILURE: Build failed with an exception.

  • What went wrong: Execution failed for task ‘:compileJava’. > invalid source release: 1.8

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

BUILD FAILED

Total time: 1.095 secs ~/myproject $

Hi Mark

If you issue gradle -version it will tell you the JVM it has picked up.  Just to be certain, could you please run the command please and see what version its reporting?  Here’s the output when I run on mine (most of my builds are against Java 8 and run fine)

------------------------------------------------------------
Gradle 2.1
------------------------------------------------------------
Build time:   2014-09-08 10:40:39 UTC
Build number: none
Revision:     e6cf70745ac11fa943e19294d19a2c527a669a53
Groovy:       2.3.6
Ant:          Apache Ant™ version 1.9.3 compiled on December 23 2013
JVM:          1.8.0_20 (Oracle Corporation 25.20-b23)
OS:           Mac OS X 10.9.4 x86_64

Thanks
Kon

Yeah, sorry I didn’t include that:

------------------------------------------------------------ Gradle 2.1 ------------------------------------------------------------

Build time:

2014-09-08 10:40:39 UTC Build number: none Revision:

e6cf70745ac11fa943e19294d19a2c527a669a53

Groovy:

2.3.6 Ant:

Apache Ant™ version 1.9.3 compiled on December 23 2013 JVM:

1.8.0_20 (Oracle Corporation 25.20-b23) OS:

Mac OS X 10.10 x86_64

Ok that looks fine. I notice the only difference to mine is that you are running Yosemite.  However, before investigating that path, how are you defining the sourceCompatibility 1.8 string in your build?

Hi, thanks. Here ya go:

In build.gradle:

sourceCompatibility = 1.8 targetCompatibility = 1.8

Hi Mark
Do you have it in a compileJava block?  Eg this works fine for a simple hello world project I have.

apply plugin: 'java'
  compileJava {
  sourceCompatibility = '1.8'
}

In versions of Gradle < 2, it used to be able to pick it up declared directly as vars in the build script.  Eg just on its own not in a compileJava block:

sourceCompatibility = ‘1.8’  

Cheers
Kon

Edit Noticed the formatting came out wrong when viewed on the webpage

Hi, I tried wrapping it in a compileJava block and I still see the same issue…

compileJava {

sourceCompatibility = 1.8 }

~/myproject $ gradle clean build :clean :compileJava FAILED

FAILURE: Build failed with an exception.

  • What went wrong: Execution failed for task ‘:compileJava’. > invalid source release: 1.8

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

BUILD FAILED

Total time: 0.991 secs

add a

println System.getProperty(“java.home”)

in your build gradle; on a Mac this should output something like

/Library/Java/JavaVirtualMachines/jdk1.8.0_20.jdk/Contents/Home/jre

Ok, interesting. So, I am getting Java 7 from Gradle but Java 8 from the command line. What’s going on here?

From Gradle: /Library/Java/JavaVirtualMachines/jdk1.7.0_17.jdk/Contents/Home/jre

From Command Line: JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_20.jdk/Contents/Home

I did find this…

http://stackoverflow.com/questions/22307516/gradle-finds-wrong-java-home-even-though-its-correctly-set

What should I do?

Hi Mark

Something in your build environment is overriding your java home.

There are a few places this can happen.  For example, IntelliJ will pick up the version the JDK you have set in your Project properties if you haven’t explicitly configured it.  If you haven’t configured this it’ll default to some other JDK (in my case the JDK intellij itself is running on Java 6 and fail with the same message)

Firstly, does gradle --version still report Java 8, but the System.getProperty(“java.home”) reports path to Java 7?  Also what is the output of /usr/libexec/java_home -t CommandLine?

Do you have anything in a ~/gradle.properties file set?  

In the meantime you can specify a property in your command line to point Gradle to the right java.home to use.  For example 

gradle clean build -Dorg.gradle.java.home='/Library/Java/JavaVirtualMachines/jdk1.8.0_20.jdk/Contents/Home'



Let us know how you go

Kind Regards
Kon

1 Like

Ok awesome. I finally tracked it down to an old java home setting in my ~/.gradle/gradle.properties file! Thanks for all the help!

Fantastic. Glad to help.

Thank you. Thread helped me too!