简体   繁体   中英

ERROR: JAVA_HOME is set to an invalid directory: Please set the JAVA_HOME variable in your environment to match the location of your Java installation

When I was running "flutter doctor --android-licenses", I struggled to fix the error below,

ERROR: JAVA_HOME is set to an invalid directory: /usr/lib/jvm/java-11-openjdk-amd64

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

I thought I should post this as a question and provide a response to it as other Answers to the this question could not help me.

Please see my solution below.

  1. run the below command to choose the java version that you want to use.

    sudo update-alternatives --config java

    You will get something like this from the terminal. 在此处输入图像描述

  2. Press the number corresponding to the version of java that you want to use. Example, Press to keep the current choice[*], or type selection number: 0

  3. Copy the PATH corresponding to the java version that you chose. In my case since I chose version in Number 0, My PATH is /usr/lib/jvm/java-11-openjdk-amd64/bin/java. But leave out the last part of the PATH,ie "/bin/java". So now your PATH is /usr/lib/jvm/java-11-openjdk-amd64

  4. run the following command, where PATH value is the value copied from above. export JAVA_HOME=PATH In my case, I will run JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64

  5. run this command, copy it as it is. Don't change anything. echo $JAVA_HOME

  6. follow by this command, copy it as it is. Don't change anything. export PATH=$PATH:$JAVA_HOME/bin

  7. run this command, echo $PATH

    This will give out the full path to your $JAVA_HOME value.

    In my case, this is what I got. /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/usr/lib/jvm/java-11-openjdk-amd64/bin

  8. Now you are sorted.

  9. "flutter doctor --android-licenses", ran successfully.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM