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

12

44

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.

Sin answered 26/5, 2015 at 6:23 Comment(0)
M
25

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.

Morse answered 18/7, 2015 at 14:49 Comment(3)
<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 helpedGird
Pointed me to root cause: I forgot to set Gradle JVM in Ctrl+Alt+S/Build Tools/Gradle/Gradle JVM after upgrading JDK.Olag
hope this comment can help you out https://mcmap.net/q/389905/-flutter-build-error-quot-invalid-source-release-17-quot-when-building-app-bundleRadioactivity
S
14

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.

Sikang answered 26/5, 2015 at 17:40 Comment(2)
In my case, the PATH was correct, but the JAVA_HOME was wrongDamnable
In my case my IntelliJ project had java 1.8, but Gradle JVM was 1.7. Changed Gradle JVM in Preferences and everything works nowGruesome
C
5
  • 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

Choiseul answered 7/12, 2017 at 2:12 Comment(0)
L
4

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.

Lui answered 2/3, 2022 at 10:49 Comment(0)
C
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.

Contuse answered 26/5, 2015 at 8:3 Comment(0)
S
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.

Sedgewinn answered 30/5, 2017 at 14:18 Comment(0)
G
1

Just had the "bug" with 1.8 java

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

Gersham answered 26/7, 2017 at 0:22 Comment(0)
C
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

Concertgoer answered 20/11, 2022 at 4:13 Comment(1)
There's like 20 of these in Ghidra that I got this error on. Which build.gradle was it in your case?Thapsus
F
1

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
}
Ferwerda answered 2/7, 2023 at 5:34 Comment(0)
Z
0

You can check Java version defined in JAVA_HOME variable and build.gradle file. If both are same this issue go away.

Zaffer answered 6/5, 2024 at 21:56 Comment(0)
Y
0

I was using the Android Studio 2021 (Dolphin).

Found that Android Studio 2021 officially supports JDK versions up to JDK 11. While JDK 17 offers newer features and improvements, it might not be fully compatible with Android Studio 2021. I updated to Jellyfish its started working

Yeaton answered 30/5, 2024 at 8:40 Comment(0)
V
0

The issue comes from an unknown java version.

In my case, the sourceCompatibility and targetCompatibility wasn't enough.

I had to add this line to change with java 21:

java.toolchain.languageVersion = JavaLanguageVersion.of(21)

And also upgrade gradle version to 8.6.

Vizierate answered 24/7, 2024 at 11:31 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.