We can set JAVA_HOME on Linux in different ways. One of the prominent ways is to set the JAVA_HOME using an environment variable.
How to set JAVA_HOME on Linux:
Environment variables can be set up in three different types:
- Local Environment variables: Bind to a specific session.
- User Environment variables: Bind to a specific user.
- System Environment variables: Bind to system-wide.
1. Set JAVA_HOME with export:
export command we can set the JAVA_HOME, but as we discussed, this is volatile – as soon as we leave the terminal session, this setting will go away.
$ export JAVA_HOME=/usr/lib/jvm/jdk-1.8.0-openjdk.x86_64 $ echo $JAVA_HOME /usr/lib/jvm/jdk-1.8.0-openjdk.x86_64
1.1 Setting PATH:
2. Set JAVA_HOME environment:
We can permanently set the JAVA_HOME using
/etc/environment file. This will be applied to system-level.
$ sudo vi /etc/environment
Add the below lines to add the JAVA_HOME
Note: We should have to append the PATH variable with the existing path ($PATH:$JAVA_HOME/bin), don’t override it.
2.1 Apply the changes:
Apply the environment variables by using the
$ source /etc/environment $ echo $JAVA_HOME /usr/lib/jvm/jdk-1.8.0-openjdk.x86_64
Happy Learning 🙂