How to set JAVA_HOME for Java permanently on Ubuntu Linux

You can set your JAVA_HOME in /etc/profile. 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

source /etc/environment

After that, run the below command to add the java_home to the path variable. 

export PATH="$PATH:$JAVA_HOME/bin"

If you want it permanent, then add the above line to ~/.profile

Now, running $ java -version would return your version.