When you work on Java or any Java based project , you might need to set JAVA environment variables JAVA_HOME and PATH in Linux.
After installing new java (jdk or jre) or latest Java you may have usually find that the version of java is not exactly the same which you have installed. It might be showing you the same old version.
How To Check Current Java Version Is Set In Linux System ?
Sometimes you want to know, is there any Java version in use in the Linux System ? So we can run the given below command to quickly find the answer.
# java --version or # java -version
If in case, you do not find any Java version is showing it means either the Java is not installed or Java Environment variable is not set.
Install New Java And Set The Java Environment Variable
Download the Jave file from Oracle website.(If you have installed with tar ball then again there is no problem)
We have installed JDK rpm just for showing an example. It could be of any version (You can skip the step if you installed through tar ball)
Install JDK (java) rpm File
Run the given below command to install the JDK rpm file. If you are a Linux user, you might be already knowing that we can use rpm or yum command to install the rpm.
sudo rpm -ivh jdk-20_linux-aarch64_bin.rpm
Now check where is the latest Java (JDK or JRE) you have installed in your system. Run below given command
sudo find / -name java
How To Set Java Variable Environment
Follow the given below steps (Replace the version no. as per your new Java version installed in your system)
Step1 : Open /root/.bash_profile through your text editor. (I prefer to use vi editor)
And paste the given below two lines
export JAVA_HOME=/usr/lib/jvm/jdk-20-oracle-x64/ export PATH=/usr/lib/jvm/jdk-20-oracle-x64/bin:$PATH
Step 2 : Now enable the Java variable without system restart (On system restart it bydefault set the java variable)
Step 3: Now check the Java version,JAVA_HOME and PATH variables.It should show you correct information as you have set.
java --version echo $JAVA_HOME echo $PATH
Given below is the reference of my system’s root bash_profile file
[root@localhost ~]# cat /root/.bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
# User specific environment and startup programs
Other files and location where you can set Java variable and what are the difference
(1) /etc/profile = To set environment variable to all users
(2) $HOME/.bashrc = To set environment for login user.
(3) $HOME/.bash_profile = To set environment for login user
Note: .bash_profile is executed for login shells, while .bashrc is executed for interactive non-login shells.
(4) Create a shell script inside /etc/profile.d/ with .sh extension. and make the file executable.
(5) Create a shell script in some other location and give its path in /etc/rc.local