How to set JAVA_HOME for Java?











up vote
261
down vote

favorite
113












I've installed a version of Java. How can we set the $JAVA_HOME environment variable correctly?










share|improve this question




























    up vote
    261
    down vote

    favorite
    113












    I've installed a version of Java. How can we set the $JAVA_HOME environment variable correctly?










    share|improve this question


























      up vote
      261
      down vote

      favorite
      113









      up vote
      261
      down vote

      favorite
      113






      113





      I've installed a version of Java. How can we set the $JAVA_HOME environment variable correctly?










      share|improve this question















      I've installed a version of Java. How can we set the $JAVA_HOME environment variable correctly?







      java






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Oct 9 '15 at 11:44









      kiri

      18.8k1258104




      18.8k1258104










      asked Aug 13 '12 at 22:31









      Gaurav Agarwal

      3,253144061




      3,253144061






















          3 Answers
          3






          active

          oldest

          votes

















          up vote
          361
          down vote



          accepted










          You can set your JAVA_HOME in /etc/profile as Petronilla Escarabajo suggests. But the preferred location for JAVA_HOME or any system variable is /etc/environment.



          Open /etc/environment in any text editor like nano or gedit and add the following line:



          JAVA_HOME="/usr/lib/jvm/open-jdk"


          (java path could be different)



          Use source to load the variables, by running this command:



          source /etc/environment


          Then check the variable, by running this command:



          echo $JAVA_HOME


          Update



          Usually most linux systems source /etc/environment by default. If your system doesn't do that add the following line to ~/.bashrc (Thanks @pje)



          source /etc/environment





          share|improve this answer



















          • 4




            I'm getting: JDK Required: 'tools.jar' seems to be not in IDEA classpath. Please ensure JAVA_HOME points to JDK rather than JRE.
            – anon58192932
            Mar 21 '14 at 21:31






          • 2




            @advocate - You should probably post your own question for that. But the clue is in the message; you need to point at a JDK not a JRE.
            – David Edwards
            Mar 28 '14 at 14:57






          • 2




            When i tried to run Android Studio (that has IntelliJ IDEA as a base), i had an error message very similar to @advocate's: "'tools.jar' seems to be not in Android Studio classpath." After fiddling a lot with JAVA_HOME without success, i decided to take a look at studio.sh, the shellscript that starts Android Studio. As a wild guess, i set JDK_HOME to the same value expected for JAVA_HOME, and voila! It installed without great problems.
            – Hilton Fernandes
            Mar 7 '15 at 23:20








          • 3




            For those doing software development, don't put your JAVA_HOME in /etc/environment unless you want to reboot everytime you switch JDK versions.
            – HDave
            Sep 20 '16 at 19:03






          • 5




            As others have pointed out, this doesn't stick between terminal sessions. What I did to address this is just added the line source /etc/environment to the top of my bash config file ~/.bashrc so that it loads all my environment settings on startup. Working for me so far.
            – pje
            Mar 11 '17 at 22:31


















          up vote
          70
          down vote













          To set JAVA_HOME environment variable, do the following:




          1. Launch Terminal by pressing Ctrl+Alt+T on your keyboard.

          2. Enter the following command:

            $ gksudo gedit /etc/environment

          3. Depending on where you installed your Java, you will need to provide the full path. For this example, I installed Oracle JDK 7 in the /usr/lib/jvm/java-7-oracle directory.
            Scroll to the end of the file and enter the following:
            JAVA_HOME=/usr/lib/jvm/java-7-oracle
            export JAVA_HOME

          4. Save your file and exit gedit.

          5. Lastly, reload the system PATH with the following command:

            $ . /etc/environment


          The above method will save you the hassle in having to run the commands every time you log in to your computer.






          share|improve this answer



















          • 5




            How does . /etc/environment work?
            – Sudip Bhandari
            Sep 13 '16 at 13:36






          • 1




            Is the addition of the export command necessary in the /etc/environment ?
            – pkaramol
            Nov 23 '16 at 10:01










          • @pkaramol I've had to add export JAVA_HOME on 16.04LTS to make it load at startup.
            – adeen-s
            Jan 20 '17 at 6:20






          • 3




            @adeen-s You added export to a line in /etc/environment and it helped? That file contains variable definitions parsed as =-delimited name-value pairs; its contents are not executed as commands. (See man pam_env.) So unless you're separately treating the file as though it were a script (such as by passing /etc/environment to bash's ./source builtin), I wouldn't expect that to work.
            – Eliah Kagan
            Aug 17 '17 at 16:02










          • > How does . /etc/environment work? -- . (dot) loads commands from a file askubuntu.com/a/232938/189965
            – Roman Bekkiev
            Sep 22 at 7:32


















          up vote
          31
          down vote













          If you do not know the path and you only have openJDK installed, you can type
          update-alternatives --config java and you should find the path. To set the variable you can write JAVA_HOME=<PATH> followed by export JAVA_HOME. Notice there's no space when declaring the variable. To check if the variable is stored you simply type echo $JAVA_HOME to verify.






          share|improve this answer





















          • This seems like it would be static. If I remove openjdk-7 and install openjdk-9, won't the JAVA_HOME then point to the wrong place? How can it be made dynamic?
            – DavidJ
            Jul 20 '16 at 18:49






          • 3




            By you manually changing it. Once again, YOU are the way it becomes dynamic....
            – HDave
            Sep 20 '16 at 19:04










          • What @HDave means is that In certain cases, you may want JAVA_HOME to point to a specific java version, so making the update of JAVA_HOME dynamic may not be what you want.
            – Maciej
            Oct 9 '16 at 15:31










          • i like this answer. I tested with echo and see my path. However, I am confused why I am still getting JAVA_HOME environment variable is not set when I run mvn -version
            – Winnemucca
            Apr 11 '17 at 22:19






          • 1




            I wrote this answer back when I was more ignorant. Setting the variable as described will only affect your current terminal session, and will not be persisted. The correct way is to run update-alternatives --install <link> <name> <target> <priority> for example: update-alternatives --install /usr/bin/java java /usr/lib/jvm/default-runtime/bin/java 1
            – Erro
            May 14 '17 at 12:26










          protected by Community Mar 8 '15 at 7:48



          Thank you for your interest in this question.
          Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



          Would you like to answer one of these unanswered questions instead?














          3 Answers
          3






          active

          oldest

          votes








          3 Answers
          3






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          361
          down vote



          accepted










          You can set your JAVA_HOME in /etc/profile as Petronilla Escarabajo suggests. But the preferred location for JAVA_HOME or any system variable is /etc/environment.



          Open /etc/environment in any text editor like nano or gedit and add the following line:



          JAVA_HOME="/usr/lib/jvm/open-jdk"


          (java path could be different)



          Use source to load the variables, by running this command:



          source /etc/environment


          Then check the variable, by running this command:



          echo $JAVA_HOME


          Update



          Usually most linux systems source /etc/environment by default. If your system doesn't do that add the following line to ~/.bashrc (Thanks @pje)



          source /etc/environment





          share|improve this answer



















          • 4




            I'm getting: JDK Required: 'tools.jar' seems to be not in IDEA classpath. Please ensure JAVA_HOME points to JDK rather than JRE.
            – anon58192932
            Mar 21 '14 at 21:31






          • 2




            @advocate - You should probably post your own question for that. But the clue is in the message; you need to point at a JDK not a JRE.
            – David Edwards
            Mar 28 '14 at 14:57






          • 2




            When i tried to run Android Studio (that has IntelliJ IDEA as a base), i had an error message very similar to @advocate's: "'tools.jar' seems to be not in Android Studio classpath." After fiddling a lot with JAVA_HOME without success, i decided to take a look at studio.sh, the shellscript that starts Android Studio. As a wild guess, i set JDK_HOME to the same value expected for JAVA_HOME, and voila! It installed without great problems.
            – Hilton Fernandes
            Mar 7 '15 at 23:20








          • 3




            For those doing software development, don't put your JAVA_HOME in /etc/environment unless you want to reboot everytime you switch JDK versions.
            – HDave
            Sep 20 '16 at 19:03






          • 5




            As others have pointed out, this doesn't stick between terminal sessions. What I did to address this is just added the line source /etc/environment to the top of my bash config file ~/.bashrc so that it loads all my environment settings on startup. Working for me so far.
            – pje
            Mar 11 '17 at 22:31















          up vote
          361
          down vote



          accepted










          You can set your JAVA_HOME in /etc/profile as Petronilla Escarabajo suggests. But the preferred location for JAVA_HOME or any system variable is /etc/environment.



          Open /etc/environment in any text editor like nano or gedit and add the following line:



          JAVA_HOME="/usr/lib/jvm/open-jdk"


          (java path could be different)



          Use source to load the variables, by running this command:



          source /etc/environment


          Then check the variable, by running this command:



          echo $JAVA_HOME


          Update



          Usually most linux systems source /etc/environment by default. If your system doesn't do that add the following line to ~/.bashrc (Thanks @pje)



          source /etc/environment





          share|improve this answer



















          • 4




            I'm getting: JDK Required: 'tools.jar' seems to be not in IDEA classpath. Please ensure JAVA_HOME points to JDK rather than JRE.
            – anon58192932
            Mar 21 '14 at 21:31






          • 2




            @advocate - You should probably post your own question for that. But the clue is in the message; you need to point at a JDK not a JRE.
            – David Edwards
            Mar 28 '14 at 14:57






          • 2




            When i tried to run Android Studio (that has IntelliJ IDEA as a base), i had an error message very similar to @advocate's: "'tools.jar' seems to be not in Android Studio classpath." After fiddling a lot with JAVA_HOME without success, i decided to take a look at studio.sh, the shellscript that starts Android Studio. As a wild guess, i set JDK_HOME to the same value expected for JAVA_HOME, and voila! It installed without great problems.
            – Hilton Fernandes
            Mar 7 '15 at 23:20








          • 3




            For those doing software development, don't put your JAVA_HOME in /etc/environment unless you want to reboot everytime you switch JDK versions.
            – HDave
            Sep 20 '16 at 19:03






          • 5




            As others have pointed out, this doesn't stick between terminal sessions. What I did to address this is just added the line source /etc/environment to the top of my bash config file ~/.bashrc so that it loads all my environment settings on startup. Working for me so far.
            – pje
            Mar 11 '17 at 22:31













          up vote
          361
          down vote



          accepted







          up vote
          361
          down vote



          accepted






          You can set your JAVA_HOME in /etc/profile as Petronilla Escarabajo suggests. But the preferred location for JAVA_HOME or any system variable is /etc/environment.



          Open /etc/environment in any text editor like nano or gedit and add the following line:



          JAVA_HOME="/usr/lib/jvm/open-jdk"


          (java path could be different)



          Use source to load the variables, by running this command:



          source /etc/environment


          Then check the variable, by running this command:



          echo $JAVA_HOME


          Update



          Usually most linux systems source /etc/environment by default. If your system doesn't do that add the following line to ~/.bashrc (Thanks @pje)



          source /etc/environment





          share|improve this answer














          You can set your JAVA_HOME in /etc/profile as Petronilla Escarabajo suggests. But the preferred location for JAVA_HOME or any system variable is /etc/environment.



          Open /etc/environment in any text editor like nano or gedit and add the following line:



          JAVA_HOME="/usr/lib/jvm/open-jdk"


          (java path could be different)



          Use source to load the variables, by running this command:



          source /etc/environment


          Then check the variable, by running this command:



          echo $JAVA_HOME


          Update



          Usually most linux systems source /etc/environment by default. If your system doesn't do that add the following line to ~/.bashrc (Thanks @pje)



          source /etc/environment






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Apr 24 '17 at 18:00









          Zanna

          49.2k13123234




          49.2k13123234










          answered Aug 14 '12 at 2:17









          Manula Waidyanatha

          5,95211619




          5,95211619








          • 4




            I'm getting: JDK Required: 'tools.jar' seems to be not in IDEA classpath. Please ensure JAVA_HOME points to JDK rather than JRE.
            – anon58192932
            Mar 21 '14 at 21:31






          • 2




            @advocate - You should probably post your own question for that. But the clue is in the message; you need to point at a JDK not a JRE.
            – David Edwards
            Mar 28 '14 at 14:57






          • 2




            When i tried to run Android Studio (that has IntelliJ IDEA as a base), i had an error message very similar to @advocate's: "'tools.jar' seems to be not in Android Studio classpath." After fiddling a lot with JAVA_HOME without success, i decided to take a look at studio.sh, the shellscript that starts Android Studio. As a wild guess, i set JDK_HOME to the same value expected for JAVA_HOME, and voila! It installed without great problems.
            – Hilton Fernandes
            Mar 7 '15 at 23:20








          • 3




            For those doing software development, don't put your JAVA_HOME in /etc/environment unless you want to reboot everytime you switch JDK versions.
            – HDave
            Sep 20 '16 at 19:03






          • 5




            As others have pointed out, this doesn't stick between terminal sessions. What I did to address this is just added the line source /etc/environment to the top of my bash config file ~/.bashrc so that it loads all my environment settings on startup. Working for me so far.
            – pje
            Mar 11 '17 at 22:31














          • 4




            I'm getting: JDK Required: 'tools.jar' seems to be not in IDEA classpath. Please ensure JAVA_HOME points to JDK rather than JRE.
            – anon58192932
            Mar 21 '14 at 21:31






          • 2




            @advocate - You should probably post your own question for that. But the clue is in the message; you need to point at a JDK not a JRE.
            – David Edwards
            Mar 28 '14 at 14:57






          • 2




            When i tried to run Android Studio (that has IntelliJ IDEA as a base), i had an error message very similar to @advocate's: "'tools.jar' seems to be not in Android Studio classpath." After fiddling a lot with JAVA_HOME without success, i decided to take a look at studio.sh, the shellscript that starts Android Studio. As a wild guess, i set JDK_HOME to the same value expected for JAVA_HOME, and voila! It installed without great problems.
            – Hilton Fernandes
            Mar 7 '15 at 23:20








          • 3




            For those doing software development, don't put your JAVA_HOME in /etc/environment unless you want to reboot everytime you switch JDK versions.
            – HDave
            Sep 20 '16 at 19:03






          • 5




            As others have pointed out, this doesn't stick between terminal sessions. What I did to address this is just added the line source /etc/environment to the top of my bash config file ~/.bashrc so that it loads all my environment settings on startup. Working for me so far.
            – pje
            Mar 11 '17 at 22:31








          4




          4




          I'm getting: JDK Required: 'tools.jar' seems to be not in IDEA classpath. Please ensure JAVA_HOME points to JDK rather than JRE.
          – anon58192932
          Mar 21 '14 at 21:31




          I'm getting: JDK Required: 'tools.jar' seems to be not in IDEA classpath. Please ensure JAVA_HOME points to JDK rather than JRE.
          – anon58192932
          Mar 21 '14 at 21:31




          2




          2




          @advocate - You should probably post your own question for that. But the clue is in the message; you need to point at a JDK not a JRE.
          – David Edwards
          Mar 28 '14 at 14:57




          @advocate - You should probably post your own question for that. But the clue is in the message; you need to point at a JDK not a JRE.
          – David Edwards
          Mar 28 '14 at 14:57




          2




          2




          When i tried to run Android Studio (that has IntelliJ IDEA as a base), i had an error message very similar to @advocate's: "'tools.jar' seems to be not in Android Studio classpath." After fiddling a lot with JAVA_HOME without success, i decided to take a look at studio.sh, the shellscript that starts Android Studio. As a wild guess, i set JDK_HOME to the same value expected for JAVA_HOME, and voila! It installed without great problems.
          – Hilton Fernandes
          Mar 7 '15 at 23:20






          When i tried to run Android Studio (that has IntelliJ IDEA as a base), i had an error message very similar to @advocate's: "'tools.jar' seems to be not in Android Studio classpath." After fiddling a lot with JAVA_HOME without success, i decided to take a look at studio.sh, the shellscript that starts Android Studio. As a wild guess, i set JDK_HOME to the same value expected for JAVA_HOME, and voila! It installed without great problems.
          – Hilton Fernandes
          Mar 7 '15 at 23:20






          3




          3




          For those doing software development, don't put your JAVA_HOME in /etc/environment unless you want to reboot everytime you switch JDK versions.
          – HDave
          Sep 20 '16 at 19:03




          For those doing software development, don't put your JAVA_HOME in /etc/environment unless you want to reboot everytime you switch JDK versions.
          – HDave
          Sep 20 '16 at 19:03




          5




          5




          As others have pointed out, this doesn't stick between terminal sessions. What I did to address this is just added the line source /etc/environment to the top of my bash config file ~/.bashrc so that it loads all my environment settings on startup. Working for me so far.
          – pje
          Mar 11 '17 at 22:31




          As others have pointed out, this doesn't stick between terminal sessions. What I did to address this is just added the line source /etc/environment to the top of my bash config file ~/.bashrc so that it loads all my environment settings on startup. Working for me so far.
          – pje
          Mar 11 '17 at 22:31












          up vote
          70
          down vote













          To set JAVA_HOME environment variable, do the following:




          1. Launch Terminal by pressing Ctrl+Alt+T on your keyboard.

          2. Enter the following command:

            $ gksudo gedit /etc/environment

          3. Depending on where you installed your Java, you will need to provide the full path. For this example, I installed Oracle JDK 7 in the /usr/lib/jvm/java-7-oracle directory.
            Scroll to the end of the file and enter the following:
            JAVA_HOME=/usr/lib/jvm/java-7-oracle
            export JAVA_HOME

          4. Save your file and exit gedit.

          5. Lastly, reload the system PATH with the following command:

            $ . /etc/environment


          The above method will save you the hassle in having to run the commands every time you log in to your computer.






          share|improve this answer



















          • 5




            How does . /etc/environment work?
            – Sudip Bhandari
            Sep 13 '16 at 13:36






          • 1




            Is the addition of the export command necessary in the /etc/environment ?
            – pkaramol
            Nov 23 '16 at 10:01










          • @pkaramol I've had to add export JAVA_HOME on 16.04LTS to make it load at startup.
            – adeen-s
            Jan 20 '17 at 6:20






          • 3




            @adeen-s You added export to a line in /etc/environment and it helped? That file contains variable definitions parsed as =-delimited name-value pairs; its contents are not executed as commands. (See man pam_env.) So unless you're separately treating the file as though it were a script (such as by passing /etc/environment to bash's ./source builtin), I wouldn't expect that to work.
            – Eliah Kagan
            Aug 17 '17 at 16:02










          • > How does . /etc/environment work? -- . (dot) loads commands from a file askubuntu.com/a/232938/189965
            – Roman Bekkiev
            Sep 22 at 7:32















          up vote
          70
          down vote













          To set JAVA_HOME environment variable, do the following:




          1. Launch Terminal by pressing Ctrl+Alt+T on your keyboard.

          2. Enter the following command:

            $ gksudo gedit /etc/environment

          3. Depending on where you installed your Java, you will need to provide the full path. For this example, I installed Oracle JDK 7 in the /usr/lib/jvm/java-7-oracle directory.
            Scroll to the end of the file and enter the following:
            JAVA_HOME=/usr/lib/jvm/java-7-oracle
            export JAVA_HOME

          4. Save your file and exit gedit.

          5. Lastly, reload the system PATH with the following command:

            $ . /etc/environment


          The above method will save you the hassle in having to run the commands every time you log in to your computer.






          share|improve this answer



















          • 5




            How does . /etc/environment work?
            – Sudip Bhandari
            Sep 13 '16 at 13:36






          • 1




            Is the addition of the export command necessary in the /etc/environment ?
            – pkaramol
            Nov 23 '16 at 10:01










          • @pkaramol I've had to add export JAVA_HOME on 16.04LTS to make it load at startup.
            – adeen-s
            Jan 20 '17 at 6:20






          • 3




            @adeen-s You added export to a line in /etc/environment and it helped? That file contains variable definitions parsed as =-delimited name-value pairs; its contents are not executed as commands. (See man pam_env.) So unless you're separately treating the file as though it were a script (such as by passing /etc/environment to bash's ./source builtin), I wouldn't expect that to work.
            – Eliah Kagan
            Aug 17 '17 at 16:02










          • > How does . /etc/environment work? -- . (dot) loads commands from a file askubuntu.com/a/232938/189965
            – Roman Bekkiev
            Sep 22 at 7:32













          up vote
          70
          down vote










          up vote
          70
          down vote









          To set JAVA_HOME environment variable, do the following:




          1. Launch Terminal by pressing Ctrl+Alt+T on your keyboard.

          2. Enter the following command:

            $ gksudo gedit /etc/environment

          3. Depending on where you installed your Java, you will need to provide the full path. For this example, I installed Oracle JDK 7 in the /usr/lib/jvm/java-7-oracle directory.
            Scroll to the end of the file and enter the following:
            JAVA_HOME=/usr/lib/jvm/java-7-oracle
            export JAVA_HOME

          4. Save your file and exit gedit.

          5. Lastly, reload the system PATH with the following command:

            $ . /etc/environment


          The above method will save you the hassle in having to run the commands every time you log in to your computer.






          share|improve this answer














          To set JAVA_HOME environment variable, do the following:




          1. Launch Terminal by pressing Ctrl+Alt+T on your keyboard.

          2. Enter the following command:

            $ gksudo gedit /etc/environment

          3. Depending on where you installed your Java, you will need to provide the full path. For this example, I installed Oracle JDK 7 in the /usr/lib/jvm/java-7-oracle directory.
            Scroll to the end of the file and enter the following:
            JAVA_HOME=/usr/lib/jvm/java-7-oracle
            export JAVA_HOME

          4. Save your file and exit gedit.

          5. Lastly, reload the system PATH with the following command:

            $ . /etc/environment


          The above method will save you the hassle in having to run the commands every time you log in to your computer.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Aug 18 '12 at 21:01

























          answered Aug 13 '12 at 23:01









          Petronilla Escarabajo

          1,44877




          1,44877








          • 5




            How does . /etc/environment work?
            – Sudip Bhandari
            Sep 13 '16 at 13:36






          • 1




            Is the addition of the export command necessary in the /etc/environment ?
            – pkaramol
            Nov 23 '16 at 10:01










          • @pkaramol I've had to add export JAVA_HOME on 16.04LTS to make it load at startup.
            – adeen-s
            Jan 20 '17 at 6:20






          • 3




            @adeen-s You added export to a line in /etc/environment and it helped? That file contains variable definitions parsed as =-delimited name-value pairs; its contents are not executed as commands. (See man pam_env.) So unless you're separately treating the file as though it were a script (such as by passing /etc/environment to bash's ./source builtin), I wouldn't expect that to work.
            – Eliah Kagan
            Aug 17 '17 at 16:02










          • > How does . /etc/environment work? -- . (dot) loads commands from a file askubuntu.com/a/232938/189965
            – Roman Bekkiev
            Sep 22 at 7:32














          • 5




            How does . /etc/environment work?
            – Sudip Bhandari
            Sep 13 '16 at 13:36






          • 1




            Is the addition of the export command necessary in the /etc/environment ?
            – pkaramol
            Nov 23 '16 at 10:01










          • @pkaramol I've had to add export JAVA_HOME on 16.04LTS to make it load at startup.
            – adeen-s
            Jan 20 '17 at 6:20






          • 3




            @adeen-s You added export to a line in /etc/environment and it helped? That file contains variable definitions parsed as =-delimited name-value pairs; its contents are not executed as commands. (See man pam_env.) So unless you're separately treating the file as though it were a script (such as by passing /etc/environment to bash's ./source builtin), I wouldn't expect that to work.
            – Eliah Kagan
            Aug 17 '17 at 16:02










          • > How does . /etc/environment work? -- . (dot) loads commands from a file askubuntu.com/a/232938/189965
            – Roman Bekkiev
            Sep 22 at 7:32








          5




          5




          How does . /etc/environment work?
          – Sudip Bhandari
          Sep 13 '16 at 13:36




          How does . /etc/environment work?
          – Sudip Bhandari
          Sep 13 '16 at 13:36




          1




          1




          Is the addition of the export command necessary in the /etc/environment ?
          – pkaramol
          Nov 23 '16 at 10:01




          Is the addition of the export command necessary in the /etc/environment ?
          – pkaramol
          Nov 23 '16 at 10:01












          @pkaramol I've had to add export JAVA_HOME on 16.04LTS to make it load at startup.
          – adeen-s
          Jan 20 '17 at 6:20




          @pkaramol I've had to add export JAVA_HOME on 16.04LTS to make it load at startup.
          – adeen-s
          Jan 20 '17 at 6:20




          3




          3




          @adeen-s You added export to a line in /etc/environment and it helped? That file contains variable definitions parsed as =-delimited name-value pairs; its contents are not executed as commands. (See man pam_env.) So unless you're separately treating the file as though it were a script (such as by passing /etc/environment to bash's ./source builtin), I wouldn't expect that to work.
          – Eliah Kagan
          Aug 17 '17 at 16:02




          @adeen-s You added export to a line in /etc/environment and it helped? That file contains variable definitions parsed as =-delimited name-value pairs; its contents are not executed as commands. (See man pam_env.) So unless you're separately treating the file as though it were a script (such as by passing /etc/environment to bash's ./source builtin), I wouldn't expect that to work.
          – Eliah Kagan
          Aug 17 '17 at 16:02












          > How does . /etc/environment work? -- . (dot) loads commands from a file askubuntu.com/a/232938/189965
          – Roman Bekkiev
          Sep 22 at 7:32




          > How does . /etc/environment work? -- . (dot) loads commands from a file askubuntu.com/a/232938/189965
          – Roman Bekkiev
          Sep 22 at 7:32










          up vote
          31
          down vote













          If you do not know the path and you only have openJDK installed, you can type
          update-alternatives --config java and you should find the path. To set the variable you can write JAVA_HOME=<PATH> followed by export JAVA_HOME. Notice there's no space when declaring the variable. To check if the variable is stored you simply type echo $JAVA_HOME to verify.






          share|improve this answer





















          • This seems like it would be static. If I remove openjdk-7 and install openjdk-9, won't the JAVA_HOME then point to the wrong place? How can it be made dynamic?
            – DavidJ
            Jul 20 '16 at 18:49






          • 3




            By you manually changing it. Once again, YOU are the way it becomes dynamic....
            – HDave
            Sep 20 '16 at 19:04










          • What @HDave means is that In certain cases, you may want JAVA_HOME to point to a specific java version, so making the update of JAVA_HOME dynamic may not be what you want.
            – Maciej
            Oct 9 '16 at 15:31










          • i like this answer. I tested with echo and see my path. However, I am confused why I am still getting JAVA_HOME environment variable is not set when I run mvn -version
            – Winnemucca
            Apr 11 '17 at 22:19






          • 1




            I wrote this answer back when I was more ignorant. Setting the variable as described will only affect your current terminal session, and will not be persisted. The correct way is to run update-alternatives --install <link> <name> <target> <priority> for example: update-alternatives --install /usr/bin/java java /usr/lib/jvm/default-runtime/bin/java 1
            – Erro
            May 14 '17 at 12:26















          up vote
          31
          down vote













          If you do not know the path and you only have openJDK installed, you can type
          update-alternatives --config java and you should find the path. To set the variable you can write JAVA_HOME=<PATH> followed by export JAVA_HOME. Notice there's no space when declaring the variable. To check if the variable is stored you simply type echo $JAVA_HOME to verify.






          share|improve this answer





















          • This seems like it would be static. If I remove openjdk-7 and install openjdk-9, won't the JAVA_HOME then point to the wrong place? How can it be made dynamic?
            – DavidJ
            Jul 20 '16 at 18:49






          • 3




            By you manually changing it. Once again, YOU are the way it becomes dynamic....
            – HDave
            Sep 20 '16 at 19:04










          • What @HDave means is that In certain cases, you may want JAVA_HOME to point to a specific java version, so making the update of JAVA_HOME dynamic may not be what you want.
            – Maciej
            Oct 9 '16 at 15:31










          • i like this answer. I tested with echo and see my path. However, I am confused why I am still getting JAVA_HOME environment variable is not set when I run mvn -version
            – Winnemucca
            Apr 11 '17 at 22:19






          • 1




            I wrote this answer back when I was more ignorant. Setting the variable as described will only affect your current terminal session, and will not be persisted. The correct way is to run update-alternatives --install <link> <name> <target> <priority> for example: update-alternatives --install /usr/bin/java java /usr/lib/jvm/default-runtime/bin/java 1
            – Erro
            May 14 '17 at 12:26













          up vote
          31
          down vote










          up vote
          31
          down vote









          If you do not know the path and you only have openJDK installed, you can type
          update-alternatives --config java and you should find the path. To set the variable you can write JAVA_HOME=<PATH> followed by export JAVA_HOME. Notice there's no space when declaring the variable. To check if the variable is stored you simply type echo $JAVA_HOME to verify.






          share|improve this answer












          If you do not know the path and you only have openJDK installed, you can type
          update-alternatives --config java and you should find the path. To set the variable you can write JAVA_HOME=<PATH> followed by export JAVA_HOME. Notice there's no space when declaring the variable. To check if the variable is stored you simply type echo $JAVA_HOME to verify.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Aug 18 '12 at 21:29









          Erro

          65478




          65478












          • This seems like it would be static. If I remove openjdk-7 and install openjdk-9, won't the JAVA_HOME then point to the wrong place? How can it be made dynamic?
            – DavidJ
            Jul 20 '16 at 18:49






          • 3




            By you manually changing it. Once again, YOU are the way it becomes dynamic....
            – HDave
            Sep 20 '16 at 19:04










          • What @HDave means is that In certain cases, you may want JAVA_HOME to point to a specific java version, so making the update of JAVA_HOME dynamic may not be what you want.
            – Maciej
            Oct 9 '16 at 15:31










          • i like this answer. I tested with echo and see my path. However, I am confused why I am still getting JAVA_HOME environment variable is not set when I run mvn -version
            – Winnemucca
            Apr 11 '17 at 22:19






          • 1




            I wrote this answer back when I was more ignorant. Setting the variable as described will only affect your current terminal session, and will not be persisted. The correct way is to run update-alternatives --install <link> <name> <target> <priority> for example: update-alternatives --install /usr/bin/java java /usr/lib/jvm/default-runtime/bin/java 1
            – Erro
            May 14 '17 at 12:26


















          • This seems like it would be static. If I remove openjdk-7 and install openjdk-9, won't the JAVA_HOME then point to the wrong place? How can it be made dynamic?
            – DavidJ
            Jul 20 '16 at 18:49






          • 3




            By you manually changing it. Once again, YOU are the way it becomes dynamic....
            – HDave
            Sep 20 '16 at 19:04










          • What @HDave means is that In certain cases, you may want JAVA_HOME to point to a specific java version, so making the update of JAVA_HOME dynamic may not be what you want.
            – Maciej
            Oct 9 '16 at 15:31










          • i like this answer. I tested with echo and see my path. However, I am confused why I am still getting JAVA_HOME environment variable is not set when I run mvn -version
            – Winnemucca
            Apr 11 '17 at 22:19






          • 1




            I wrote this answer back when I was more ignorant. Setting the variable as described will only affect your current terminal session, and will not be persisted. The correct way is to run update-alternatives --install <link> <name> <target> <priority> for example: update-alternatives --install /usr/bin/java java /usr/lib/jvm/default-runtime/bin/java 1
            – Erro
            May 14 '17 at 12:26
















          This seems like it would be static. If I remove openjdk-7 and install openjdk-9, won't the JAVA_HOME then point to the wrong place? How can it be made dynamic?
          – DavidJ
          Jul 20 '16 at 18:49




          This seems like it would be static. If I remove openjdk-7 and install openjdk-9, won't the JAVA_HOME then point to the wrong place? How can it be made dynamic?
          – DavidJ
          Jul 20 '16 at 18:49




          3




          3




          By you manually changing it. Once again, YOU are the way it becomes dynamic....
          – HDave
          Sep 20 '16 at 19:04




          By you manually changing it. Once again, YOU are the way it becomes dynamic....
          – HDave
          Sep 20 '16 at 19:04












          What @HDave means is that In certain cases, you may want JAVA_HOME to point to a specific java version, so making the update of JAVA_HOME dynamic may not be what you want.
          – Maciej
          Oct 9 '16 at 15:31




          What @HDave means is that In certain cases, you may want JAVA_HOME to point to a specific java version, so making the update of JAVA_HOME dynamic may not be what you want.
          – Maciej
          Oct 9 '16 at 15:31












          i like this answer. I tested with echo and see my path. However, I am confused why I am still getting JAVA_HOME environment variable is not set when I run mvn -version
          – Winnemucca
          Apr 11 '17 at 22:19




          i like this answer. I tested with echo and see my path. However, I am confused why I am still getting JAVA_HOME environment variable is not set when I run mvn -version
          – Winnemucca
          Apr 11 '17 at 22:19




          1




          1




          I wrote this answer back when I was more ignorant. Setting the variable as described will only affect your current terminal session, and will not be persisted. The correct way is to run update-alternatives --install <link> <name> <target> <priority> for example: update-alternatives --install /usr/bin/java java /usr/lib/jvm/default-runtime/bin/java 1
          – Erro
          May 14 '17 at 12:26




          I wrote this answer back when I was more ignorant. Setting the variable as described will only affect your current terminal session, and will not be persisted. The correct way is to run update-alternatives --install <link> <name> <target> <priority> for example: update-alternatives --install /usr/bin/java java /usr/lib/jvm/default-runtime/bin/java 1
          – Erro
          May 14 '17 at 12:26





          protected by Community Mar 8 '15 at 7:48



          Thank you for your interest in this question.
          Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



          Would you like to answer one of these unanswered questions instead?



          Popular posts from this blog

          Ellipse (mathématiques)

          Quarter-circle Tiles

          Mont Emei