How To Set Java Home Path In Linux?
Java is popular programming language and framework in the IT industry. There are a lot of applications developed with Java. Java requires Java Development Kit to develop applications. Compiled java applications can run on systems those are install Java Runtime Environment a.k.a JRE. We will look in this post how to set and check JRE or JDK or Java Home Path.
There are two main different version of JDK and JRE. Original vendor is Oracle which distributes and sets standards of Java. Second is open version of JDK and JRE those are supported by Oracle. In this post we will install open version which named openjdk-9-jre-headless .
$ sudo apt-get install openjdk-9-jre-headless -y
Check Java Status
We will look if java path already set simple running java command which is compiler for Java applications.
Update Locate Database
This is auxiliary operation to use locate OpenJDK location we update locate database
$ sudo updatedb
Find JDK Location
We will look where is JDK located and also get JDK version. This operation can be used Oracle JDK too. Just the JDK directory name will be different.
$ locate openjdk
Set Java Home
As we see there is a lot of output for JDK search we can see that path of JDK is /usr/lib/jvm/java-9-openjdk-amd64 because it is located in usr lib. Now we will set PATH for Java
$ export JAVA_HOME="/usr/lib/jvm/java-9-openjdk-amd64"
Make Java Home Configuration Persistent
The previous step makes JAVA HOME available for subshells but if the system is restarted the path will be lost. So we need to make this path definition persistent. There are different methods to make persistent. We will look single and simple one. We will add path definition to the system-wide environment definition configuration. This operation requires root privileges.
$ echo "JAVA_HOME='/usr/lib/jvm/java-9-openjdk-amd64'" >> /etc/environment
Alternative method without the need of root privileges is adding Java home path definition to the normal user .bashrc file like below.
$ echo "JAVA_HOME='/usr/lib/jvm/java-9-openjdk-amd64'" >> .bashrc