34

I am using :

  1. gradle-2.3
  2. javac -version = 1.7
  3. jre = 1.7
  4. regedit shows it is pointing to 1.7.

But I am still getting below error

Execution failed for task ':compileJava'. > invalid source release: 1.7

Please let me know how to fix it.

mpromonet
  • 11,326
  • 43
  • 62
  • 91
BdEngineer
  • 2,929
  • 4
  • 49
  • 85

9 Answers9

18

You can set the JDK Version used by gradle for the build by adding a "gradle.properties" file to your project. Add the following property:

org.gradle.java.home = <Path to the JDK you want to use for your project>

I agree with the previous answer that you also should check if the JDK and the sourceCompatibility match.

Jens
  • 241
  • 2
  • 9
  • <3, it was driving me crazy - btw fot those with same problem I messed up my project by installing older version of java to support Adobe Illustrator on Mac and this helped – Srneczek Feb 09 '16 at 19:50
  • 5
    Pointed me to root cause: I forgot to set Gradle JVM in `Ctrl+Alt+S/Build Tools/Gradle/Gradle JVM` after upgrading JDK. – Tomáš Záluský Apr 27 '21 at 15:33
13

You say you are running with Java 7, but are you really sure?

Because as far as I know that error occurs precisely when you are using a source / target level that is not supported by the JVM you are running gradle with. So if I were to take a guess I'd say that gradle seems to think your JDK doesn't support Java 7 (so its JDK 6 or lower)

Maybe double check that

a) Gradle itself is running with JDK 7. If you run gradle from within Eclipse using the STS gradle tooling, it will use the workspace default JRE to run gradle. Check that it is at least a JDK 7. (Go to "Windows >> Preferences >> Java >> Installed JRE". The JRE with a 'check mark' is the one Gradle will run with).

b) Gradle may accidentally pick up another JDK to compile stuff with if it finds an environment variable 'JAVA_HOME'. So double check that it isn't pointing to a JDK 6 or lower.

Kris
  • 3,898
  • 1
  • 23
  • 32
4
  • To check if the $JAVA_HOME is really to point the default java

    echo $JAVA_HOME /usr/lib/jvm/java-1.7.0-openjdk
    
  • Check Java version

    java -version
    openjdk version "1.8.0_151"
    
  • If misaligned like above, to modify $JAVA_HOME in the /etc/profile (or alternatively the .profile/.bashprofile/ in your user's home directory), let point to

    JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk
    
  • BTW: solved at Centos7 to fix Invalid release 1.8

3

If you have tried all solutions viz -

  1. Fixing your JAVA_HOME and Java run time
  2. Fixing you Gradle JVM version in preferences
  3. Setting up the org.gradle.java.home =

still you face the issue then check your build.gradle file and look if sourceCompatibility = '17' is present alone. If yes comment it out.

Krishna
  • 5,194
  • 2
  • 28
  • 33
2

Try the following in your build gradle:

apply plugin: 'java'

sourceCompatibility = JavaVersion.VERSION_1_7
targetCompatibility = JavaVersion.VERSION_1_7

If this does not work please provide the contents of your build.gradle.

Julian Pieles
  • 3,880
  • 2
  • 23
  • 33
1

In my case Gradle ran on JRE instead of JDK (wrong set of JAVA_HOME). Pointing my JAVA_HOME to the root of the JDK fixed the build. (Of course, assuming your PATH has %JAVA_HOME%\bin)

Reason:

The JDK has the javac for compiling java while the JRE does not.

Pam Stums
  • 176
  • 8
1

Just had the "bug" with 1.8 java

Solved: Findbugs showed me an ü in a method title ( not allowed in graddle)

sparksen
  • 23
  • 5
1

For me I had to change the value of sourceCompatibility property in build.gradle file to be something less than or equal current version [I've v. 16, and changed the sourceCompatibility to v. 11]

enter image description here

KhogaEslam
  • 2,528
  • 1
  • 20
  • 21
0

It's suggesting that the code is being compiled with a JDK version that doesn't support the specified source level. In mine case I am having jdk version 11 configured on my system but initially in gradle it was 17. So, I need to change it to version 11 in mine build.gradle. (Note:- change both sourceCompatibility and targetCompatibility with the version of jdk you are having in ur system)

java {
sourceCompatibility(JavaVersion.VERSION_11) // 17 ->11
targetCompatibility(JavaVersion.VERSION_11)  // 17-> 11
}