19

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
  • 10,379
  • 42
  • 54
  • 85
BdEngineer
  • 2,475
  • 3
  • 38
  • 73

7 Answers7

12

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,792
  • 1
  • 19
  • 31
11

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
  • 171
  • 1
  • 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
  • 2
    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
3

to check if the $JAVA_HOME is really to point the default java # echo $JAVA_HOME /usr/lib/jvm/java-1.7.0-openjdk # 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

1

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,720
  • 2
  • 22
  • 30
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
  • 138
  • 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
  • 4
1

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
  • 4,564
  • 2
  • 25
  • 32