250

I have set JAVA_HOME in my system C:\Program Files\Java\jdk1.8.0_ 131\bin; and I am trying to run cordova command( cordova build ) on command prompt, but I'm getting the error bellow. I have tried all the ways, but not able to resolve this issue:

ERROR: JAVA_HOME is set to an invalid directory: C:\Program Files\Java\jdk1.8.0_ 131\bin;

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

13
  • 51
    JAVA_HOME should be upto C:\Program Files\Java\jdk1.8.0_ 131 only Commented Jul 19, 2017 at 6:42
  • 4
    I removed still am getting the same error Commented Jul 19, 2017 at 6:46
  • 5
    after changing variable you need to restart command line..also remove double quotes at start and end of variable Commented Jul 19, 2017 at 6:52
  • Search for something like Setting and getting windows environment variables from the command prompt to find how to set and check environment variables. Commented Jul 19, 2017 at 6:52
  • 2
    Its working fine but am getting another errorFAILURE: Build failed with an exception. * What went wrong: Unable to start the daemon process. This problem might be caused by incorrect configuration of the daemon. For example, an unrecognized jvm option is used. Please refer to the user guide chapter on the daemon at docs.gradle.org 3.3/userguide/gradle_daemon.html Please read the following process output to find out more: ----------------------- Error occurred during initialization of VM Could not reserve enough space for 2097152KB object heap Commented Jul 19, 2017 at 7:07

28 Answers 28

330

You should set it with C:\Program Files\Java\jdk1.8.0_12.

\bin should not be included.

Sign up to request clarification or add additional context in comments.

4 Comments

Yes i had open.
For Linux, something like that is needed: export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk/
how do I know which version of jre I have installed (I'm getting this error in react native app)
Try : flutter config --jdk-dir="C:\Program Files\... the file path. if you work in flutter
89

JAVA_HOME should point to the home jdk directory, and not to jdk/bin directory.

You need to set the JAVA_HOME like this:

JAVA_HOME="C:\Program Files\Java\jdk1.8.0_131"

1 Comment

In my case the environment variable had wrong path JAVA_HOME=C:\Program Files\Java\jre1.8.0_311 Actual java path was C:\Program Files\Java\jre1.8.0_321
57

On linux and mac:

  1. To find the path java -XshowSettings:properties -version 2>&1 > /dev/null | grep 'java.home'
  2. result e.g. java.home = /usr/lib/jvm/java-11-openjdk-amd64
  3. copy: /usr/lib/jvm/java-11-openjdk-amd64
  4. Add line in your config bash file (e.g.~/.zshrc): export JAVA_HOME=<resultFirstStep>
  5. Be sure to run source ~/.zshrc (or source ~/.bashrc for bash, etc.)

3 Comments

Also worked for Mac
Great! thanks! If the problem arises from a Flutter project, also do flutter config --jdk-dir="${JAVA_HOME}" (provided one has set the env-var JAVA_HOME earlier, as you suggest. It would be nice if Flutter could read the env-var and not require this special notification.
Now that worked buddy
45

Remove the \bin, and also remove the ; at the end. After restart the cmd and run.

5 Comments

Your comment saved my life. Thanks. Added semi colon at the end which leads to endless search in google. After removing the semi colon at the end its works like charm. Thanks
Your answer is the correct answer. Even after I removed \bin , I got error because of ";".
Removing semicolon is the trick
How to remove semi colon
This man save me :D
35

Try the following:

  • Remove \bin from JAVA_HOME path.
  • Open new command line window.
  • Run your command as an administrator.

1 Comment

When setting up React Native, I had to remove \bin from the JAVA_HOME path.
22

JAVA_HOME should be C:\Program Files\Java\jdk1.8.0_172 don't include semi-colon(;) or bin in path. Any jdk version above 7 will work. Also, you need to re-start the cmd

Comments

22

On Window 10, the problem was with the semicolon ;.

Go to edit the system environment variables and delete the semicolon at the end of JAVA_HOME value C:\Program Files\Java\jdk1.8.0_144

In other words, convert this C:\Program Files\Java\jdk1.8.0_12; to C:\Program Files\Java\jdk1.8.0_12

You might have to delete your entry in the Windows Dialog and create a new one. If you ever had multiple entries and get the bigger Form view, Windows automatically inserts a ; at the end of each entry, even if you only have one entry left.

Comments

13

set JAVA_HOME as C:\Program Files\Java\jdk1.8.0_172

do not use semicolon ";" with above JAVA_HOME value

To verify that it is correctly set, Go to command prompt check for C:.......>sdkmanager --list

a complete list will appear that will let you now that it is successfully saved.

else it will show error that JAVA_HOME path is not correctly set.

Comments

10

Please remove /bin and even semi colon ; from JAVA_HOME to resolve.

Comments

8

Change JAVA_HOME in Environment variable to this

C:\Program Files\Android\Android Studio\jbr

and since Electric Eel use jbr copy jbr file and replace it into jre then restart ur pc. It worked for me after days of tedious try and error days

Comments

7

First try removing the '\bin' from the path and set the home directory JAVA_HOME as below: JAVA_HOME : C:\Program Files\Java\jdk1.8.0_131

Second Update System PATH:

  1. In “Environment Variables” window under “System variables” select Path
  2. Click on “Edit…”
  3. In “Edit environment variable” window click “New”
  4. Type in %JAVA_HOME%\bin

Third restart your docker.

Refer to the link for setting the java path in windows.

Comments

6

If you have updated android studio (2023), then this will surely work for you:

Copy the path of jbr:


C:\Program Files\Android\Android Studio\jbr


and then under system variables click on new, type JAVA_HOME in the first box and in the second box paste the path of jbr.

Comments

4

After setting the JAVA_HOME variable, run android studio as administrator

Comments

4

You need to set with only C:\Program Files\Java\jdk1.8.0_12.

And check with using new cmd. It will be updated

Comments

4

I found this while googling the issue with android licences when installing Flutter.

For Linux, update Your ~/.bashrc (or other shell config) file by adding this line at the end:

JAVA_HOME=/usr/local/android-studio/jre

Note: I installed Android studio by following the official installation guide for Linux (link).

For Windows, use the android studio's path to the jre directory and update the environment variable with the right value.

Then, run flutter doctor --android-licenses and press y to review the licences which are not accepted. Press y for each of the licences. It will ask Accept? (y/N): at the end of the license file. After You accept all unaccepted licences, You should see:

All SDK package licenses accepted

Comments

4

While copying the JDK PATH, exclude \bin folder in it.

e.g.

C:\Program Files\Java\jdk-15.0.2

your path should look like blow, you just need to edit it and remove \bin folder

C:\Program Files\Java\jdk-15.0.2\bin

Comments

3

Check for duplicate entries of JAVA_HOME in system variables.

In my case none of the above answers worked but I had two entries for JAVA_HOME and removing one worked.

1 Comment

would you add more details ?
3

Removing /bin from path works for me C:\Program Files\Java\jdk-11

Comments

2

i think you need to remove the ';' from the end of the java path.

1 Comment

That didn't work, When setting the JAVA_HOME, you should not include the /bin. If you are setting it to the path only you should include the bin
2

I was using jenv

In my case, I was using jenv but did not set the java version, so the path that jenv was pointing actually a Java installation.

When I ran echo $JAVA_HOME, it showed /Users/username/.jenv/versions/system, and there was no such file or directory.

Set a version of java

I just had to do jenv global 15 or jenv local 15, to specify what version I wanted to actually use, and JAVA_HOME changed when I re-ran echo $JAVA_HOME.

Comments

1

Had this issue after many years using java with AS. Might help someone to just restart android studio after following the instructions above.

  1. Set JAVA_HOME to the destination of your java installation or file
  2. Don't use /bin in your path
  3. Important: Restart android studio. (Not restart and invalidate cache). Close AS down and restart.

Comments

1

For a Flutter project, in addition to what @LuanLuanLuan proposed (https://stackoverflow.com/a/68471280/385390), also do:

flutter config --jdk-dir="${JAVA_HOME}"

provided the env-var JAVA_HOME was set earlier, as suggested.

It would be nice if Flutter could read the JAVA_HOME env-var and not require this special notification.

Once you are there, do not forget to do:

flutter --disable-analytics
flutter doctor --disable-analytics

Comments

1

in my case, in macOS and for flutter:

in zshrc or bash, save this:

export JAVA_HOME=<yourJdkPath>

in terminal, set this:

flutter config --jdk-dir="${JAVA_HOME}"

without flutter config command, and only setting java home, I even could not run counter app, as expected. Applying both solved my problem.

Comments

0

First, you need to make sure you installed JDK and then set the path to it. If you have not installed it, you can download and install it from here: https://www.oracle.com/java/technologies/downloads/

Comments

0

In Windows 10 the ; is automarically added. In order to remove this when opening the edit environmental variable window, press the Edit Text... and then you will be able to remove the ; at the end.

After that the flutter doctor --android-licenses worked correctly.

Comments

0

On Windows 11 some applications require you to set both the JAVA_HOME variable and include Path to JDK Bin Directory.

In this case, you need to edit both Java Home and the Path on the User variables and System Variables. System variables/User variables don't need the \bin reference but the Path needs the \bin reference

Setting JAVA_HOME

Simply copy-paste the file path to the JDK folder exluding \bin part:

C:\Program Files\Java\jdk-21.0.1

enter image description here

Setting the Path

You need to include \bin reference part:

%JAVA_HOME%\bin

enter image description here

%JAVA_HOME% points to the above variable file path i.e. C:\Program Files\Java\jdk-21.0.1 and then \bin is appended at the end.

Confirmation

To confirm it has been set correctly, restart Android Studio or CMD and type this command on the terminal:

java -version

You should see this this message:

java version "21.0.1" 2023-10-17 LTS Java(TM) SE Runtime Environment (build 21.0.1+12-LTS-29) Java HotSpot(TM) 64-Bit Server VM (build 21.0.1+12-LTS-29, mixed mode, sharing)

Comments

0

This path is not valid anymore:

C:\Program Files\Java\jdk1.8.0_12.

New Path:

C:\Program Files\Android\AndroidStudio\jbr

Search in windows search bar

  1. Edit the system environment variable and hit enter
  2. Click on Environment variables.
  3. Click on New
  4. New user variable dialog open
  5. write variable name : JAVA_HOME
  6. write variable value: C:\Program Files\Android\AndroidStudio\jbr
  7. click on OK
  8. then restart android studio
  9. open terminal
  10. check Gradle version
./gradlew -version

Comments

-1

i have set like this in vs code for project.its working for me

JAVA_HOME="/Library/Java/JavaVirtualMachines/jdk1.8.0_321.jdk/Contents/Home"

1 Comment

this answer is not helping to understand why there was an error in the windows system/user environment variables. This is very specific to the internals of VSCode...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.