Introduction
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.
Check which Java Version is default set In Linux System ?
Sometimes you want to know, is there any Java version in use in the Linux System ? Basically we will check what is the default Java version is set in the system.
Given below command is 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.
Always download the latest and stable release for installing.
sudo rpm -ivh jdk-20_linux-aarch64_bin.rpm
How To Set Java Variable Environment
Follow the given below steps
Step1 : Open /root/.bash_profile through your text editor. (we prefer to use vi editor).
(Replace the version no. as per your new Java version installed in your system. Check the JDK Folder name and its path before updating the .bash_profile file.)
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)
source /root/.bash_profile
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 our system’s root bash_profile file
[root@localhost ~]# cat /root/.bash_profile
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/bin
export PATH
export JAVA_HOME=/usr/lib/jvm/jdk-20-oracle-x64/
export PATH=/usr/lib/jvm/jdk-20-oracle-x64/bin:$PATH
[root@localhost ~]#
Set Java variables in other files and location
There are some locations in Linux where we can also set Java variables. Following are the paths.
- $HOME/.bashrc = To set environment for login user.
- $HOME/.bash_profile = To set environment for login user
- /etc/profile = To set environment variable to all users
- Create a shell script inside /etc/profile.d/ with .sh extension. and make the file executable.
- Create a shell script in some other location and give its path in /etc/rc.local
Note: .bash_profile is executed for login shells, while .bashrc is executed for interactive non-login shells.
hello
my path is a little strange, shoude it look like this.
echo $PATH
/usr/java/jdk1.8.0_162/bin:/usr/java/jdk1.8.0_162/bin:/usr/java/jdk1.8.0_162:/usr/java/jdk1.8.0_162/jre:/usr/java/jdk1.8.0_162/jre:/usr/java/jdk1.8.0_162/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:/home/niklas/.local/bin:/home/niklas/bin:/root/bin:/root/bin:/root/bin:/root/bin:/root/bin:/root/bin
You are the best! Thanks
Thanks for your nice tutorials
wtf… finally someone that reaaly knows what he’s doing!!! thanks a lot!
Nice its work thank you !!!!!!!
You are most welcome Vishwnath,
Regards
Sharad
I got an error here but don’t know what to do. A little help will be highly appreciated.
JAVA_HOME is defined, but the ‘java’ executable
cannot be found in:
/usr/java/latest/bin
This is a fatal error.
Verify your JAVA_HOME environment variable is set
in ./config-vars.sh
Hello John,
Run the command –
You will get output from above command. Set the correct path of latest Java.
Note: In case you are using latest Java, just check with
java -version
without setting any variable. Means just install and run the java with latest version.Regards
Sharad
Thanks so much for helping me get the path set after a couple hours of reading everyone’s incorrect opinions
Thankyou Ted,
Glad to know the tutorial helped you.
Regards
Sharad