Ubuntu – How to set JAVA_HOME for Java


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

Best 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 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:



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
Related Question