I've installed a version of Java. How can we set the $JAVA_HOME environment variable correctly?
5 Answers
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 - 2When 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– Hilton Fernandes2015-03-07 23:20:31 +00:00Commented Mar 7, 2015 at 23:20
- 6For those doing software development, don't put your JAVA_HOME in /etc/environment unless you want to reboot everytime you switch JDK versions.Dave– Dave2016-09-20 19:03:43 +00:00Commented Sep 20, 2016 at 19:03
- 3This is a temporary solution, as others pointed out. No one would want to run source every time they restart their bash.yuranos– yuranos2017-02-19 21:49:31 +00:00Commented Feb 19, 2017 at 21:49
- 7As others have pointed out, this doesn't stick between terminal sessions. What I did to address this is just added the line
source /etc/environmentto the top of my bash config file~/.bashrcso that it loads all my environment settings on startup. Working for me so far.pje– pje2017-03-11 22:31:11 +00:00Commented Mar 11, 2017 at 22:31 - 2@sedulam I updated the answerManula Waidyanatha– Manula Waidyanatha2017-04-20 06:20:21 +00:00Commented Apr 20, 2017 at 6:20
To set JAVA_HOME environment variable, do the following:
- Launch Terminal by pressing Ctrl+Alt+T on your keyboard.
- Enter the following command:
$gksudo gedit /etc/environment - 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-oracledirectory.
Scroll to the end of the file and enter the following:
JAVA_HOME=/usr/lib/jvm/java-7-oracle
export JAVA_HOME - Save your file and exit gedit.
- 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.
- 7How does . /etc/environment work?Sudip Bhandari– Sudip Bhandari2016-09-13 13:36:11 +00:00Commented Sep 13, 2016 at 13:36
- 1Is the addition of the
exportcommand necessary in the/etc/environment?pkaramol– pkaramol2016-11-23 10:01:24 +00:00Commented Nov 23, 2016 at 10:01 - @pkaramol I've had to add export JAVA_HOME on 16.04LTS to make it load at startup.adeen-s– adeen-s2017-01-20 06:20:13 +00:00Commented Jan 20, 2017 at 6:20
- 4@adeen-s You added
exportto a line in/etc/environmentand it helped? That file contains variable definitions parsed as=-delimited name-value pairs; its contents are not executed as commands. (Seeman pam_env.) So unless you're separately treating the file as though it were a script (such as by passing/etc/environmentto bash's./sourcebuiltin), I wouldn't expect that to work.Eliah Kagan– Eliah Kagan2017-08-17 16:02:18 +00:00Commented Aug 17, 2017 at 16:02 - 1> How does . /etc/environment work? -- . (dot) loads commands from a file askubuntu.com/a/232938/189965Roman Bekkiev– Roman Bekkiev2018-09-22 07:32:37 +00:00Commented Sep 22, 2018 at 7:32
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.
- 1This 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– DavidJ2016-07-20 18:49:40 +00:00Commented Jul 20, 2016 at 18:49
- 3By you manually changing it. Once again, YOU are the way it becomes dynamic....Dave– Dave2016-09-20 19:04:32 +00:00Commented Sep 20, 2016 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– Maciej2016-10-09 15:31:53 +00:00Commented Oct 9, 2016 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 -versionWinnemucca– Winnemucca2017-04-11 22:19:04 +00:00Commented Apr 11, 2017 at 22:19
- 4I 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 1Erro– Erro2017-05-14 12:26:57 +00:00Commented May 14, 2017 at 12:26
This is the best option if you always want to use the latest one installed.
Nowadays (Ubuntu 23.10) you should probably have it installed through apt like:
sudo apt install default-jdk # or default-jre In this case, you should find it at /usr/lib/jvm/default-java.
It's a symlink that points to whichever the current JVM is on the same folder.
So, just run this to add the environment as a separate file:
sudo nano /etc/environment.d/90java.conf Add this line and save:
JAVA_HOME="/usr/lib/jvm/default-java" Then reboot or login again, or source /etc/environment.d/90java.conf to load it on the current shell.
- Wasn't there for me. If you use zulu it's under /usr/lib/jvm/zulu17Alkanshel– Alkanshel2024-06-12 01:29:55 +00:00Commented Jun 12, 2024 at 1:29
IMHO the best approach (which doesn't require any non-standard software to be installed) is to put the following near the end of your .bashrc:
jvm=$(update-alternatives --query java | grep '^Value:' | cut -f2 -d' ') ; export JAVA_HOME=${jvm%/*/*} This should keep JAVA_HOME up to date even if the software gets updated (say to JDK 21 or 25); although you'll have to log out/in sessions for this to take effect.
Testing (after logging out/in the session):
echo $JAVA_HOME /usr/lib/jvm/java-17-openjdk-amd64 $JAVA_HOME/bin/java --version openjdk 17.0.15 2025-04-15 OpenJDK Runtime Environment (build 17.0.15+6-Ubuntu-0ubuntu120.04) OpenJDK 64-Bit Server VM (build 17.0.15+6-Ubuntu-0ubuntu120.04, mixed mode, sharing) Explanation:
update-alternatives --query java: displays Ubuntu's available JVMs (more than one can be installed; only one is the default; this is indicated by the line that starts withValue:)grep '^Value:' | cut -f2 -d' ': takes the output of the previous command and extracts the path of the defaultjavaexecutable (that launches the JVM).export JAVA_HOME=${jvm%/*/*}:JAVA_HOMEmust point to the root of the JVM installation, not thejavaexecutable. The Bash parameter substitution${jvm%/*/*}strips the last two components from thejavaexecutable's path returned by the previous command.