Linux / Ubuntu - set system JAVA_HOME env variable
In this short article we woukld like to show how to set JAVA_HOME
environment variable in Ubutnu 18.04.
Quick solution (create /etc/profile.d/jdk
file with following content):
export JAVA_HOME="/usr/lib/jvm/java-14-oracle"
export PATH=$PATH:$JAVA_HOME/bin
Where: /usr/lib/jvm/java-14-oracle
should be changed to Java instalation directory path.
Detailed description
The approach presented in article should be working on Debian or latest Ubuntu too.
Hint: for java installed from package name ended with
*.-set-default
postfix,JAVA_HOME
should be set automatically, e.g.
sudo apt-get install oracle-java14-set-default
Simple steps:
1. find java directory path:
ls -al /usr/lib/jvm/
Output:
total 32
drwxr-xr-x 5 root root 4096 paź 9 14:25 .
drwxr-xr-x 143 root root 4096 paź 9 14:25 ..
lrwxrwxrwx 1 root root 25 lut 20 2019 default-java -> java-1.11.0-openjdk-amd64
lrwxrwxrwx 1 root root 21 sty 15 2020 java-1.11.0-openjdk-amd64 -> java-11-openjdk-amd64
-rw-r--r-- 1 root root 2044 lip 15 22:45 .java-1.11.0-openjdk-amd64.jinfo
drwxr-xr-x 7 root root 4096 sie 18 09:56 java-11-openjdk-amd64
drwxr-xr-x 9 root root 4096 paź 9 14:24 java-14-oracle
-rw-r--r-- 1 root root 3559 paź 9 14:24 .java-14-oracle.jinfo
lrwxrwxrwx 1 root root 20 sie 3 03:16 java-1.8.0-openjdk-amd64 -> java-8-openjdk-amd64
-rw-r--r-- 1 root root 2712 sie 3 03:16 .java-1.8.0-openjdk-amd64.jinfo
drwxr-xr-x 7 root root 4096 sie 19 12:26 java-8-openjdk-amd64
Note: we want to use:
/usr/lib/jvm/java-14-oracle
(it is line 8).
2. add /etc/profile.d/jdk
file with following content:
export JAVA_HOME="/usr/lib/jvm/java-14-oracle"
export PATH=$PATH:$JAVA_HOME/bin
Note: you can add it on the beginning of
/etc/profile
too - this approach is not recommended because of messing in scripts politics.
3. rebot operating system with:
sudo reboot
4. test JAVA_HOME
variable with:
echo $JAVA_HOME
Output:
/usr/lib/jvm/java-14-oracle