Search code examples
javaflutterjava-home

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.


Solution

    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. enter image description here

    2. Press the number corresponding to the version of java that you want to use. Example, Press "Enter" 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,i.e "/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.