Home » Android » java – Android Studio can't find JAVA_HOME

java – Android Studio can't find JAVA_HOME

Posted by: admin May 14, 2020 Leave a comment

Questions:

I can’t seem to get my Android applications to compile due to a really annoying exception from Android Studio:

]()![enter image description here

Execution failed for task ':myapp-services:compileDebugJava'.
    Cannot find System Java Compiler. Ensure that you have installed a JDK (not just a JRE) and configured your JAVA_HOME system variable to point to the according directory.

I’m on Ubuntu 12.04, and I’m running Oracle’s Java 7 JRE/JDK.

In ~/.bashrc:

export JAVA_HOME="/usr/lib/jvm/java-7-oracle/"

In android-studio/bin/studio.sh:

export JAVA_HOME="/usr/lib/jvm/java-7-oracle/"

Output of javac -version:

javac 1.7.0_51

Output of java -version:

java version "1.7.0_51"
Java(TM) SE Runtime Environment (build 1.7.0_51-b13)
Java HotSpot(TM) 64-Bit Server VM (build 24.51-b03, mixed mode)

Output of printenv JAVA_HOME:

/usr/lib/jvm/java-7-oracle

Output of which javac:

/usr/bin/javac

I have also logged out and logged back in again to my session.

I’m really not sure what I’m missing here. Why can’t Android Studio compile my application?

How to&Answers:

I solved it by updating build.gradle (Top-Level) with

classpath 'com.android.tools.build:gradle:2.1.2'

Answer:

I found a solution in a different answer:

rm ~/.AndroidStudioPreview/config/options/jdk.table.xml

What seems to have happened is that something was configured for a previous version of Android Studio and this configuration lived too long 🙂

Answer:

I was getting this error too. After many hours trying to solve it, I’ve managed to solve the problem.

In my case, executing the app from command line did the trick!

Here’s what I did (I’m using Windows 10). Please, run these commands on the root of the project (where we have the app directory):

1) gradlew assembleDebug

  • Gradle will execute its tasks and you should see the message: BUILD SUCCESSFUL

2) adb install -r app\build\outputs\apk\app-debug-unaligned.apk

  • After executing this line, you’ll see the message:
    [100%] /data/local/tmp/app-debug-unaligned.apk
    pkg: /data/local/tmp/app-debug-unaligned.apk
    Success

3) adb shell am start -n package_name/package_name.MainActivity, where “package_name” must be replaced by your package name (you can find it on AndroidManifest.xml). If “MainActivity” is not your launcher activity, replace it by your own activity.

  • You’ll see the message: Starting: Intent { cmp=package_name/.MainActivity }, where “package_name” is your app’ package name.

It will install the app on your device and after that, Android Studio seems to find the JAVA_HOME variable again. You should be able to run the app by hitting the “Run” button on Android Studio.

Hope it helps someone!

Answer:

I have solved it by updating classpath of (build.gradle (Project:projectName))

classpath 'com.android.tools.build:gradle:2.2.3' 

Answer:

update build.gradle module

    classpath 'com.android.tools.build:gradle:3.0.1'

Solved my issue

Answer:

Go To Settings->Project Structure->JDK path and change it to the system JDK instead of Studio JDK, you will be up and running.

Answer:

Just downloaded the latest version (android-studio-ide-1641136.dmg) and I just changed the Info.plist file content. It’s originally configured to use 1.6* as of Java version. I have 1.8 installed, so just changed to 1.8* and worked.

I am on Mac Yosemite.

Answer:

I have found a problem with the Android Studio studio.bat file
and here it is

    ::------------------------------------------------------
    :: Locate a JDK installation directory which will be used to run the IDE.
    :: Try (in order): ANDROID_STUDIO_JDK, ..\jre, JDK_HOME, JAVA_HOME.
    :: ---------------------------------------------------------------------
    IF EXIST "%ANDROID_STUDIO_JDK%" SET JDK=%ANDROID_STUDIO_JDK%
    IF NOT "%JDK%" == "" GOTO jdk
    IF EXIST "%~dp0\..\jre" SET JDK=%~dp0\..\jre
    IF NOT "%JDK%" == "" GOTO jdk
    IF EXIST "%JDK_HOME%" SET JDK=%JDK_HOME%
    IF NOT "%JDK%" == "" GOTO jdk
    IF EXIST "%JAVA_HOME%" SET JDK=%JAVA_HOME%
    IF "%JDK%" == "" GOTO error

Do you get the problem in the last 2 lines of code?
It seems that if your environmental variable is %JAVA_HOME% the batch file will GOTO error and an error message will be displayed and that’s it NO ANDROID STUDIO FOR YOU, even though %JAVA_HOME% contains a valid path but if the environmental variable is something like %ANDROID_STUDIO_JDK% like mine is because I created it myself then it will work


PROPOSED SOLUTIONS

(1) Edit the batch file
Where you see this
IF EXIST "%JAVA_HOME%" SET JDK=%JAVA_HOME%
IF "%JDK%" == "" GOTO error

Change it to this
IF EXIST "%JAVA_HOME%" SET JDK=%JAVA_HOME%
IF NOT "%JDK%" == "" GOTO jdk
IF "%JDK%" == "" GOTO error

(2) Create an environmental variable named %ANDROID_STUDIO_JDK%
as shown here
https://kb.wisc.edu/cae/page.php?id=24500

Answer:

Check echo $JAVA_HOME

if it doesn’t gives you correct path. follow setting JAVA_HOME path on Ubuntu

if it gives you correct path set then you need to update your gradle plugin version (It worked for me) and rebuild project.