How to add JAVA_HOME on Ubuntu?

On Ubuntu, we can add JAVA_HOME environment variable in /etc/environment file.

Note
/etc/environment system-wide environment variable settings, which means all users use it. It is not a script file, but rather consists of assignment expressions, one per line. We need admin or sudo to modify this it.

Further Reading Ubuntu – EnvironmentVariables

1. JAVA_HOME

1.1 Edit /etc/environment file with a text editor like vim or nano, need root or sudo.


$ sudo vim /etc/environment

1.2 Add JAVA_HOME at the next line, and points to a specified JDK folder directly.

/etc/environment

PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games"
JAVA_HOME=/usr/lib/jvm/adoptopenjdk-11-hotspot-amd64

1.3 Reflect the changes in the current session.


$ source /etc/environment

$ echo $JAVA_HOME

/usr/lib/jvm/adoptopenjdk-11-hotspot-amd64

Note
The new changes will disappear if we close the current session or reopen a new terminal because a new shell does not trigger the /etc/environment. Try to restart the Ubuntu or login again; the new changes in /etc/environment will apply automatically.

References

author image

mkyong

Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities. Read all published posts by

Comments

avatar