Android Studio , on windows : JAVA_HOME is not set and no 'java' command could be found in your PATH
Asked Answered
S

3

7

I am trying to build my Android app on the command line using Gradle commands. On executing the .gradlew build I am getting the error below:

ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.

Please set the JAVA_HOME variable in your environment to match the location of your Java installation.

but I think I have already set the JAVA_HOME Path. If I echo %JAVA_HOME% in a separate cmd.exe window I get:

C:\Program Files\Android\Android Studio\jre

The location above is set as the JDK location

I have to point the JAVA_HOME to the JRE provided by Android and not the system Java, right?

Also, when I run the java command, it shows

'java' is not recognized as an internal or external command, operable program or a batch file

java  -version
java version "1.8.0_271"
Java(TM) SE Runtime Environment (build 1.8.0_271-b09)
Java HotSpot(TM) Client VM (build 25.271-b09, mixed mode, sharing)

Please clarify?

Signatory answered 21/9, 2021 at 8:42 Comment(5)
you are pointing to a jre instead of jdk. Anyway the path there should be something like /Users/username/Library/Android/sdk. On Windows it will be a bit differentDovev
what happens if you run java -version?Dovev
i get this : 'java' is not recognized as an internal or external command, operable program or batch file.Signatory
so your JAVA_HOME is not correctDovev
when you install Android Studio, it installs a JDK. Find where it is. I can't tell you because it looks like you are using Windows and I'm using MacOSDovev
L
2

I understood your problem, add your JDK (Not JRE) path to the PATH variable in environment variables. Then run the java command, and it should run. And whenever you post a question, please do make sure that you mention details like what happens when you run java command :)

Limewater answered 21/9, 2021 at 13:36 Comment(2)
Thanks works fine now ! I get this after i set the path when i do where is java : >where is java C:\Program Files (x86)\Common Files\Oracle\Java\javapath\java.exe C:\Program Files\Android\Android Studio\jre\bin\java.exe (bot sure which one it is taking, I hope the second one)Signatory
For starting, Android Studio takes its own JDK. For building and gradle, it takes from your PATH or JAVA_HOME variable. It doesn't matter if the JDK is same for both. Anyways, happy to help :)Limewater
C
12

Quick answer:: change JAVA_HOME variable to \jbr enter image description here

  • More details:

Seems like it has to do with Android studio installer. if you will look closely into your "C:\Program Files\Android\Android Studio\jre\bin" folder it will probably be empty!

now check "C:\Program Files\Android\Android Studio\jbr\bin" you will see many java related ".exe" files and other files. change JAVA_HOME variable to point to the jbr.

make sure your "Path" variable also contain a %JAVA_HOME%\bin variable: enter image description here

Caucus answered 26/5, 2023 at 21:41 Comment(1)
Worked for me. This is the most up to date answer. \jre being practically empty is not mentioned in older comments.Hemphill
B
4

PATH environment variable must indicate to the \bin directory of your JAVA_HOME, for example they should look like this:

JAVA_HOME: "C:\Program Files\Java\jdk1.8.0_301"
PATH: "C:\Program Files\Java\jdk1.8.0_301\bin"

Note that the PATH variable has other values too and you must append your value to it.

Bourdon answered 29/9, 2021 at 13:36 Comment(0)
L
2

I understood your problem, add your JDK (Not JRE) path to the PATH variable in environment variables. Then run the java command, and it should run. And whenever you post a question, please do make sure that you mention details like what happens when you run java command :)

Limewater answered 21/9, 2021 at 13:36 Comment(2)
Thanks works fine now ! I get this after i set the path when i do where is java : >where is java C:\Program Files (x86)\Common Files\Oracle\Java\javapath\java.exe C:\Program Files\Android\Android Studio\jre\bin\java.exe (bot sure which one it is taking, I hope the second one)Signatory
For starting, Android Studio takes its own JDK. For building and gradle, it takes from your PATH or JAVA_HOME variable. It doesn't matter if the JDK is same for both. Anyways, happy to help :)Limewater

© 2022 - 2024 — McMap. All rights reserved.