gradlew uses wrong Java version

10,744

Solution 1

A copy of the latest OpenJDK comes bundled with Android Studio 2.2 and higher, and this is the JDK version we recommend you use for your Android projects.

https://developer.android.com/studio/intro/studio-config.html#jdk

This however, is only for building your apps within Android Studio, not from the terminal with gradlew, otherwise, it'll use whatever is on your OS's $PATH variable.

In order to use the embedded JDK, you at least need to set JAVA_HOME, for example on Linux/Mac,

$ export JAVA_HOME=/path/to/AndroidStudio/jdk  # TODO: Find this
$ ./gradlew

My recommendation, however, is to use the mechanism for your OS for installing Java.

For easy Java library management (on Linux & Mac), you can try using sdkman

Solution 2

gradle.properties can be updated to point to the embedded JDK that comes with Android Studio in Mac OS:

org.gradle.java.home=/Applications/Android Studio.app/Contents/jre/jdk/Contents/Home

Solution 3

The issue is resolved by adding :

/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin

at the start of the PATH variable . What i have found is that: There are three java locations.

  1. Java that comes with mac (/usr/bin/java)
  2. Java downloaded from oracle . ( /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin)
  3. Java embedded with Android Studio (/Applications/Android\ Studio.app/Contents/jre/jdk/Contents/Home/bin/)

In my case the gradlew was always using the java from MAC (point number 1). So when i added the " java dowloaded from oracle (point 2)" at the start of path variable it started using the one from oracle and my gradlew command ran.

Share:
10,744
Pardeep Kumar
Author by

Pardeep Kumar

Updated on July 30, 2022

Comments

  • Pardeep Kumar
    Pardeep Kumar almost 2 years

    I am working on an android application using Kotlin. I am running a kotlin linter through command line ./gradlew klint.
    But it gives me an error:

    Gradle 4.4 requires Java 7 or later to run. You are currently using Java 6.

    When I check the project structure, I am using the embedded JDK.
    I also updated android studio to latest version. I do not know how to update the embedded JDK to latest version.

    java version

    "1.6.0_65" Java(TM) SE Runtime Environment (build 1.6.0_65-b14-468) Java HotSpot(TM) 64-Bit Server VM (build 20.65-b04-468, mixed mode)

    which java - /usr/bin/java

    echo $PATH - /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/username/Library/Android/sdk/platform-tools

    echo $JAVA_HOME - empty

    • fab
      fab about 6 years
      what is the output of java -version?
    • Pardeep Kumar
      Pardeep Kumar about 6 years
      java version "1.6.0_65" Java(TM) SE Runtime Environment (build 1.6.0_65-b14-468) Java HotSpot(TM) 64-Bit Server VM (build 20.65-b04-468, mixed mode)
    • OneCricketeer
      OneCricketeer about 6 years
      I'm fairly sure the emdedded JDK is at least Java 7. gradlew uses PATH anyway, not the embedded Java of Android Studio
    • OneCricketeer
      OneCricketeer about 6 years
      Please edit the question with output of which java and echo $PATH and echo $JAVA_HOME
    • OneCricketeer
      OneCricketeer about 6 years
      Why don't you just install Java on your OS? You are not required to use the embedded JDK, nor can I find any documentation that recommends it.
    • Pardeep Kumar
      Pardeep Kumar about 6 years
      @cricket_007 Yes t i will ry to install on my OS and then check
    • fab
      fab about 6 years
    • Martin Marconcini
      Martin Marconcini about 6 years
      Did you run /usr/libexec/java_home -V ? Then just download the latest from Oracle.
  • Pardeep Kumar
    Pardeep Kumar about 6 years
    Thankyou for your help to point out that gradlew uses java from system not from android studio
  • Phong Nguyen
    Phong Nguyen over 4 years
    for windows: C:/Program Files/Android/Android Studio/jre
  • CitrusO2
    CitrusO2 over 4 years
    i had to use set JAVA_HOME=/path/to/jdk for windows
  • Matthew Sisinni
    Matthew Sisinni over 2 years
    It might have changed, but mine was at /Applications/Android Studio.app/Contents/jre/Contents/Home/ (no /jdk/ folder)