• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
sharadchhetri

sharadchhetri

Tutorials On Linux, Unix & Open Source

  • Home
  • Linux Commands
  • Resources
    • Learn Linux
  • My WordPress plugins

How to set JAVA environment variables in Linux

June 3, 2013 by Sharad Chhetri 10 Comments

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)

 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 my 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 ~]#

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

Share this:

  • Twitter
  • Facebook
  • More
  • Print
  • Email
  • LinkedIn
  • Reddit
  • Tumblr
  • Pinterest
  • Pocket
  • Telegram
  • WhatsApp
  • Mastodon

Related posts:

  1. Bash Code Injection Vulnerability via Specially Crafted Environment Variables
  2. How to install Java (OpenJDK) on Ubuntu Linux
  3. Install and set Java on Ubuntu 14.04 LTS with tar ball file
  4. Recover Linux Grub Password in linux rescue mode CentOS/Red Hat
  5. rsync all files,hidden files,symlinks,hardlinks to remotes Linux Server
  6. 4 Different commands to find system uptime in linux
  7. How to protect from port scanning and smurf attack in Linux Server by iptables
  8. Learn Linux Date Command With Examples
  9. How to add/install Ubuntu fonts in CentOS/Red Hat Linux
  10. How to page scroll up/down in Linux terminal

Filed Under: Linux, Tips And Tricks Tagged With: java

Reader Interactions

Comments

  1. Niklas says

    February 14, 2020 at 12:15 pm

    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

    Reply
  2. Diego says

    June 1, 2017 at 5:38 am

    You are the best! Thanks

    Reply
  3. salil says

    April 28, 2017 at 7:16 am

    Thanks for your nice tutorials

    Reply
  4. Roger says

    March 14, 2017 at 1:08 pm

    wtf… finally someone that reaaly knows what he’s doing!!! thanks a lot!

    Reply
  5. Vishwnath Patil says

    July 30, 2015 at 12:46 pm

    Nice its work thank you !!!!!!!

    Reply
    • sharad chhetri says

      July 30, 2015 at 4:20 pm

      You are most welcome Vishwnath,

      Regards
      Sharad

      Reply
  6. John says

    May 11, 2015 at 5:48 pm

    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

    Reply
    • sharad chhetri says

      May 11, 2015 at 6:08 pm

      Hello John,

      Run the command –

      find / -name java
      

      You will get output from above command. Set the correct path of latest Java.

      export JAVA_HOME=/usr/java/jdk1.7.0_21
      export PATH=/usr/java/jdk1.7.0_21/bin:$PATH
      

      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

      Reply
  7. Ted says

    March 2, 2014 at 9:55 pm

    Thanks so much for helping me get the path set after a couple hours of reading everyone’s incorrect opinions

    Reply
    • sharad chhetri says

      March 3, 2014 at 3:02 am

      Thankyou Ted,
      Glad to know the tutorial helped you.

      Regards
      Sharad

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Our Social Media Presence

  • Facebook
  • GitHub
  • Twitter

Linux Command

What is Linux Internal And External Command

Linux Basic Commands With Examples For Every Beginner

tr command to convert lines to space , tab and vertical tab

smbpasswd command not found on CentOS 7 and RHEL 7

Solution : semanage command not found

Unix / Linux : How to print duplicate lines from file

More Posts from this Category

You Might Like These Articles!

simplecodesyntax wordpress plugin

SimpleCodeSyntax : My Another WordPress Plugin

Install Nginx

How To Install Nginx On Ubuntu 22.04 LTS

Install Latest Git package in Ubuntu Operating System

How To Always Install Latest Git Package In Ubuntu Operating System

Bash script for installing VirtualBox on Ubuntu 22.04 LTS Desktop

Install VirtualBox On Ubuntu 22.04 LTS Desktop (Bash Script)

libfuse

dlopen(): error loading libfuse.so.2 – Got Error On Ubuntu

Failed to open/create the internal network

VirtualBox Error: Failed to open/create the internal network

Always Useful Tips And Tricks

error could not configure a c compiler Linux

Password prompt in single user mode is not secure : CentOS/Red Hat

How to see system load average in terminal with graphical representation

Allow wget and yum in iptable

Edit the crontab without using crontab -e

send email after mysql backup through bash script in simple way

Replace keyword with its filename without extension : bash script

Explore 90+ Article On "Linux Tips And Tricks"

Copyright © 2023 ยท
The material in this site cannot be republished either online or offline, without our permission.
Proudly Blogging From Bharat.

  • Contact
  • About Me
  • My WordPress plugins
  • Privacy Policy