Java is a 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 installed Java Runtime Environment a.k.a JRE. We will look in this post on how to set and check JRE or JDK or Java Home Path.
There are two main different versions of JDK and JRE. The original vendor is Oracle which distributes and sets standards of Java. Second is an open version of JDK and JRE that are supported by Oracle. In this post, we will install the open version named as
$ sudo apt-get install openjdk-9-jre-headless -y
Check Java Status
We will look if java path already sets simple running java command which is a 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 at where is JDK located and also get the 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 the 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
An alternative method without the need for 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