231

I ran the following comment:

./gradlew app:installDebug

only to be met with the log:

FAILURE: Build failed with an exception.

* What went wrong:
Could not determine java version from '11.0.2'.

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

* Get more help at https://help.gradle.org

When I ran ./gradlew --version, I got the following:

FAILURE: Build failed with an exception.

* What went wrong:
Could not determine java version from '11.0.2'.

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

* Get more help at https://help.gradle.org

My .../gradle/wrapper/gradle-wrapper.properties contains the following including distributionUrl=.../gradle-4.1-rc-1-all.zip:

distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-rc-1-all.zip
6
  • 5
    according to this bug report , is it possible your gradle wrapper is older? Commented Jan 25, 2019 at 2:22
  • 2
    You run the command ./gradlew which initiate the gradle version associated with this specific project. There is no guarantee that the project's version is the same as the one you have installed (5.1.1)
    – tryman
    Commented Jan 25, 2019 at 3:09
  • 1
    Is that version info the result of gradle --version or ./gradlew --version? Gradle based projects often use a wrapper to make it easier for different developers to use the same Gradle version to build the project; this also makes builds more stable.
    – Slaw
    Commented Jan 25, 2019 at 4:18
  • that's the version of gradle --version
    – lesley2958
    Commented Jan 25, 2019 at 5:24
  • Could you also go to your project folder and open the file: .../gradle/wrapper/gradle-wrapper.properties ? Open it with a simple text editor. The "distributionUrl" inside should tell us what the wrapper's version is.
    – tryman
    Commented Jan 27, 2019 at 18:21

30 Answers 30

201

There are two different Gradle applications in your system.

  1. the system-wide Gradle
    This application is invoked by gradle (arguments).

  2. the gradle-wrapper
    The gradle-wrapper is specific to every project and can only be invoked inside the project's directory, using the command ./gradlew (arguments).

Your system-wide gradle version is 5.1.1 (as the OP explained in the comments, running the command gradle --version returned version 5.1.1).

However, the failure is the result of a call to the gradle-wrapper (./gradlew). Could you check your project's gradle wrapper version? To do that, execute ./gradlew --version inside your project's folder, in the directory where the gradlew and gradlew.bat files are.

Update 1:
As running ./gradlew --version failed, you can manually check your wrapper's version by opening the file:

(project's root folder)/gradle/wrapper/gradle-wrapper.properties

with a simple text editor. The "distributionUrl" inside should tell us what the wrapper's version is.

Update 2: As per the OP's updated question, the gradle-wrapper's version is 4.1RC1.
Gradle added support for JDK 11 in Gradle 5.0. Hence since 4.1RC does not support running on JDK 11 this is definitely a problem.

The obvious way, would be to update your project's gradle-wrapper to version 5.0.
However, before updating, try running gradle app:installDebug. This will use your system-wide installed Gradle whose version is 5.1.1 and supports running on Java 11. If this works, then your buildscript (file build.gradle) is not affected by any breaking changes between v.4.1RC1 and v.5.1.1 and you can then update your wrapper by executing from the command line inside your project's folder: gradle wrapper --gradle-version=5.1.1 [*].

If gradle app:installDebug fails to execute correctly, then maybe you need to upgrade your Gradle buildscript. For updating from v.4.1RC1 to 5.1.1, the Gradle project provides a guide (1, 2) with breaking changes and deprecated features between minor releases, so that you can update gradually to the latest version.

Alternatively, if for some reason you can't or don't want to upgrade your Gradle buildscript, you can always choose to downgrade your Java version to one that Gradle 4.1RC1 supports running on.

[*] As correctly pointed out in the answer by @lupchiazoem, use gradle wrapper --gradle-version=5.1.1 (and not ./gradlew as I had originally posted there by mistake). The reason is Gradle runs on Java. You can update your gradle-wrapper using any working Gradle distribution, either your system-wide installed Gradle or the gradle-wrapper itself. However, in this case your wrapper is not compatible with your installed Java version, so you do have to use the system-wide Gradle (aka gradle and not ./gradlew).

11
  • 2
    @tryman thanks! for me it was the your update 2 that helped me. As a general rule of thumb I think I'd check the gradle version going forth. Commented Feb 21, 2019 at 15:26
  • 1
    you don't state exactly what is the CAUSE of the error. Different versions? Commented Jun 10, 2019 at 12:17
  • 1
    What? I thought Update 2 was pretty clear. She was trying to run the Gradle wrapper of version 4.1RC1 with JDK11. Gradle added support for JDK11 at version 5.0. So what's your confusion @JoãoPimentelFerreira? That was the cause, the rest is the solution on how to update the project's wrapper to a later version that does support JDK11.
    – tryman
    Commented Jun 10, 2019 at 12:34
  • 1
    Now it's my turn to be confused. If you indeed have both the gradle wrapper in your project folder and a system-wide Gradle installation in place, the above step by step solution should work for you. Did you try upgrading your wrapper by executing inside your project folder the update command gradle wrapper --gradle-version=5.0? Replace 5.0 with your desired version, I think now the latest is 5.4
    – tryman
    Commented Jun 10, 2019 at 12:58
  • 1
    However a word of caution:Between version 3.3 of your wrapper an any version 5.x there are bound to be some breaking changes that you may or may not encounter. That's why in my answer I point you to first build your project with your systemwide Gradle to verify that everything runs smoothly with the version of 5.x you want to upgrade to before updating your project's wrapper. If in a hurry (I sense urgency in your tone) you can always use the system gradle directly or downgrade to an older JDK and catch up on upgrading your wrapper later.
    – tryman
    Commented Jun 10, 2019 at 13:02
51

As distributionUrl is still pointing to older version, upgrade wrapper using:

gradle wrapper --gradle-version 5.1.1

Note: Use gradle and not gradlew

1
  • 7
    Or just replace the version in gradle/wrapper/gradle-wrapper.properties distributionUrl=https\://services.gradle.org/distributions/gradle-5.1.1-all.zip Version 6 with JDK12 doesn't seem to work. 5.1.1 works fine, thank you. Commented Dec 14, 2019 at 10:41
32

Updating gradle/wrapper/gradle-wrapper.properties with the following version fixed it for me:

distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip
20

In my case the JAVA_HOME variable was set to /usr/lib/jvm/jdk-11.0.2/. It was sufficient to unset the variable like this:

$ export JAVA_HOME=

2
  • 1
    This solved my problem too. jenv might have started my problems initially.
    – jokarl
    Commented Mar 20, 2019 at 6:51
  • for me GRADLE_HOME was updated(to 5.4.1``) but not gradle version(still pointing to older version 4.3) which is weird but restarting terminal worked fine. something weird with source ~/.bash_profile Commented Jun 10, 2019 at 23:54
15

tl;dr: downgrade java by running update-alternatives


My system gradle version was 4.4.1, and the gradle wrapper version was 4.0. After running the command given by several other answers:

gradle wrapper --gradle-version 4.4.1

I still had the same error:

FAILURE: Build failed with an exception.

* What went wrong:
Could not determine java version from '11.0.4'.

It turns out java 11 wasn't supported until gradle 4.8, and my software repositories only had 4.4.1. (Also, upgrading to newer gradle version might have been incompatible with the package I was trying to compile.)

The answer was to downgrade java. My system actually had java8 already installed, and it was easy to switch between java versions by running this command and following the instructions:

sudo update-alternatives --config java
0
6

I had the same problem here. In my case I need to use an old version of JDK and I'm using sdkmanager to manage the versions of JDK, so, I changed the version of the virtual machine to 1.8.

sdk use java 8.0.222.j9-adpt

After that, the app runs as expected here.

5

To put long answer short, upgrade your gradlew using the system gradle tool. Note that the below upgrade works even if your system gradle version is < 5.

gradle wrapper --gradle-version=5.1.1
5

Because your wrapper version does not support 11+ you can make simple trick to cheat newer version of InteliJ forever.

  press3x Shift -> type "Switch Boot JDK" -> and change for java 8. 

Or If you want to work with java 11+ you simply have to update wrapper version to 4.8+

1
  • 3
    Just a heads up DO NOT try this solution on Android Studio. I will break it. Commented Aug 21, 2019 at 20:02
4

I had the same issue in Windows. My gradle configuration was to set use JDK1.8, but JAVA_HOME was configured to use another JDK.

Solution :

Set JAVA_HOME properly based on your configured JDK in gradle

enter image description here

1
  • This was the only solution that worked fine for me on current Windows 10. Sdkman as proposed here should do the trick too, but is a bit tricky under Windows.
    – Cadoiz
    Commented Sep 30, 2021 at 7:38
4

I've had the same issue. Upgrading to gradle 5.0 did the trick for me.

This link provides detailed steps on how install gradle 5.0

3

In my case, I was trying to build and get APK for an old Unity 3D project (so that I can play the game in my Android phone). I was using the most recent Android Studio version, and all the SDK packages I could download via SDK Manager in Android Studio. SDK Packages was located in

C:/Users/Onat/AppData/Local/Android/Sdk 

And the error message I got was the same except the JDK (Java Development Kit) version "jdk-12.0.2" . JDK was located in

C:\Program Files\Java\jdk-12.0.2

And Environment Variable in Windows was JAVA_HOME : C:\Program Files\Java\jdk-12.0.2

After 3 hours of research, I found out that Unity does not support JDK 10, as told here. My suggestion is:

  1. Uninstall unwanted JDK if you have one installed already.
  2. Head here
  3. Login to/Open a Oracle account if not already logged in.
  4. Download the older but functional JDK 8 for your computer set-up(32 bit/64 bit, Windows/Linux etc.)
  5. Install the JDK. Remember the installation path.
  6. If you are using Windows, Open Environment Variables and change Java Path via Right click My Computer/This PC>Properties>Advanced System Settings>Environment Variables>New>Variable Name: JAVA_HOME>Variable Value: [YOUR JDK Path, Mine was "C:\Program Files\Java\jdk1.8.0_221"]
  7. In Unity 3D, press Edit > Preferences > External Tools and fill in the JDK path (Mine was "C:\Program Files\Java\jdk1.8.0_221").
  8. Also, in the same pop-up, edit SDK Path. (Get it from Android Studio > SDK Manager > Android SDK > Android SDK Location.)
  9. If needed, restart your computer for changes to take effect.
1
  • 1
    I was using and older version of Gradle (4.1) on my system and trying to use Java 11. I guess that Java version was not supported by Gradle 4.1. So I just updated Gradle with brew upgrade gradle and it installed the latest (7.0) version of Gradle. Life is good again. Commented Apr 16, 2021 at 19:43
3

Navigate to Project/gradle/wrapper/gradle-wrapper.properties and manually changed the distributionUrl to distributionUrl=https\://services.gradle.org/distributions/gradle-5.1.1-bin.zip and it worked Hope this helps someone.

3

Just goto your "project folder/gradle/gradle-wrapper.properties" and in distibutionUrl where your gradle version is metioned, check the new updated gradle version from here and change it in "distributionUrl" and done!

1
  • It almost worked for me, I modified the version in the default link from what it was to latest like this. https\://services.gradle.org/distributions/gradle-2.7-bin.zip --> https\://services.gradle.org/distributions/gradle-7.4.2-bin.zip
    – Tirterra
    Commented Jun 9, 2022 at 7:21
3

Cause of the error

The error is caused by an incompatibility between the JDK and Gradle

Solution

Check this table to get the exact relationship between java & gradle versions

enter image description here

Version not found in the table

In some cases when your gradle version is not listed in the table, you will need to try one by one jdk versions until the error disappears

For example, in my case I tried with gradle-4.5 and only with java 1.8 I was able to solve the same error log: Could not determine java version from '11'.

How to configure

So if you are on linux and you need to automate some gradle steps like build, test, etc you only need to download gradle and jdk, then export these variables:

Linux

export JAVA_HOME=/foo/bar/Apps/openjdk-8uxyz
export PATH=$PATH:$JAVA_HOME/bin

export GRADDLE_HOME=/foo/bar/Apps/gradle-4.5
export PATH=$PATH:$GRADDLE_HOME/bin

Windows

set JAVA_HOME=/foo/bar/Apps/openjdk-8uxyz
set PATH=%PATH%;%JAVA_HOME%/bin

set GRADDLE_HOME=/foo/bar/Apps/gradle-4.5
set PATH=%PATH%;%GRADDLE_HOME%/bin

Download

You could download gradle (gradle-x.y.z-all.zip) from

You could download or install java from:

2

I had a similar problem: my default gradle wrapper was version 4.x, while the support for higher versions of Java has been added in Gradle 5.

I've updated my gradlew as described here: https://docs.gradle.org/current/userguide/gradle_wrapper.html#sec:upgrading_wrapper

TLTD:

./gradlew wrapper --gradle-version 5.6.2
2

I have the similar issue in Windows with redhat java-11 version, edited the gradle-wrapper.properties and updated the distributionUrl as below with gradle-6.5.1-bin.zip and its working for me.

distributionUrl=https\://services.gradle.org/distributions/gradle-6.5.1-bin.zip
2

If you've android studio version 4.2.2 its starts as default Java 11.0.8

Check your version Android Studio -> About Android Studio

Change 11.0.8 to JDK 8

1- Install the JDK8

2- Install the Choose Runtime plugin

3- Open action tab: Command + Shift + A ⇧⌘A

actiontab

4- Choose runtime and select jdk 8 on list

selectjdk

2

Head over toandroid/gradle/wrapper/gradle-wrapper.properties and update the distributionUrl with the latest gradle version.

Go to https://gradle.org/releases/ to get the latest version, then edit it here.

https\://services.gradle.org/distributions/gradle-7.3.3-all.zip

7.3.3 being the latest version release.

https\://services.gradle.org/distributions/gradle-#latest_gradle-version#-all.zip

1

I solved this by clicking on File -> Project Structure then changed the JDK Location to Use Embedded JDK (Recommended)

1

I was facing the same issue in Docker setup, while I was trying to install Gradle-2.4 with JDL 11.0.7. I have to install a later version to fix the issue.

Here is the Working Dockerfile

FROM openjdk:11.0.7-jdk
RUN apt-get update && apt-get install -y unzip
WORKDIR /gradle
RUN curl -L https://services.gradle.org/distributions/gradle-6.5.1-bin.zip -o gradle-6.5.1-bin.zip
RUN unzip gradle-6.5.1-bin.zip
ENV GRADLE_HOME=/gradle/gradle-6.5.1
ENV PATH=$PATH:$GRADLE_HOME/bin
RUN gradle --version

1

Getting this error when doing a cordova build android --release

I was able to resolve this, after trying so so many different things, by simply doing :

npm install cordova -g   # to upgrade to version 10.0.0

cordova platform rm android
cordova platform add android    # to upgrade to android version 9.0.0
1
  • cordova requirements is also a way to display the installed gradle version Commented Aug 16, 2022 at 16:19
0

I ran into a similar issue. I deleted these:

  • libraries and caches from the .idea folder ( YourApp > .idea > .. ) AND
  • contents of the build folder.

    then rebuild.

* DON'T FORGET TO BACKUP YOUR PROJECT FIRST *

0

The simplest for a quick and dirty test is to just use an override, for example:

> JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_162.jdk/Contents/Home/ ./gradlew build

since the wrapper checks for JAVA_HOME you can just pick any you want on each run.

0

You might missed to install maven please install the maven then please checked. it will work after ....

1
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Sep 13, 2021 at 13:57
0

As others have mentioned, Java 11 support was added in Gradle 5. The solution is to upgrade your Gradle wrapper:

  1. Go here to figure out the latest version of Gradle: https://gradle.org/releases/

  2. Upgrade the Gradle wrapper

    • If you have Gradle installed on your machine:

      gradle wrapper --gradle-version 7.2
      
    • If you don't have Gradle installed:

      1. Modify gradle/wrapper/gradle-wrapper.properties

        Update distributionUrl with the latest version of Gradle, e.g.

        distributionUrl=https\://services.gradle.org/distributions/gradle-7.2-bin.zip
        
      2. Upgrade the remaining wrapper files (gradle-wrapper.jar, gradlew, gradlew.bat)

        ./gradlew wrapper --gradle-version 7.2
        

Since you're upgrading from an old version of Gradle, you'll probably get errors like these:

Could not find method compile() for arguments ...
Could not find method testCompile() for arguments ...

To fix these, edit build.gradle and replace every instance of compile with implementation and replace testCompile with testImplementation.

0

I ran into the same issue in Ubuntu 18.04.3 LTS. In my case, apt installed gradle version 4.4.1. The already-install java version was 11.0.4

The build message I got was

Could not determine java version from '11.0.4'.

At the time, most of the online docs referenced gradle version 5.6, so I did the following:

sudo add-apt-repository ppa:cwchien/gradle
sudo apt update
sudo apt upgrade gradle

Then I repeated the project initialiation (using gradle init with the defaults). After that, ./gradlew build worked correctly.

I later read a comment regarding a change in format of the output from java --version that caused gradle to break, which was fixed in a later version of gradle.

0

got to project file.. gradle/wrapper/gradlewrapper.properties

there you can change the value of distributionurl to what ever the lastest version is. (Found on docs.gradle.org)

0

I am running Gradle on Windows.

In my case, I already had the older Gradle version present in my system and set it into the "Environment Variable".

That is the reason the error occurred.

So I removed the old version, set the new one in "Environment Variable" - path variable and its starts working.

0

if you're working on a legacy project; Up the gradle version to 6.9 and the Gradle plugin version to 4.2.2. This is the Gradle version to which you can safely turn up without anything breaking.

0

This is because your gradle version is imcompatible with Java 11. Either upgrade your gradle version or downgrade your Java version.

Not the answer you're looking for? Browse other questions tagged or ask your own question.