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

sharadchhetri

AI, Linux, Cloud and DevOps

  • Home
  • Resources
    • Learn Linux
    • Articles
  • My Projects & Work
    • My Nagios Plugin
    • My Dockerhub Repo
    • My WordPress plugins
    • My Github Repos
  • Facebook Page
  • Contact
  • Privacy Policy
  • About Me
You are here: Home / Articles / How to set JAVA environment variables in Linux

How to set JAVA environment variables in Linux

June 3, 2013 by Sharad Chhetri 10 Comments

Table of Contents

  • Introduction
  • Check which Java Version is default set In Linux System ?
  • Install New Java And Set The Java Environment Variable
  • Install JDK (java) rpm File
  • How To Set Java Variable Environment
  • Set Java variables in other files and location

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.

  1. $HOME/.bashrc = To set environment for login user.
  2. $HOME/.bash_profile = To set environment for login user
  3. /etc/profile = To set environment variable to all users
  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

Note: .bash_profile is executed for login shells, while .bashrc is executed for interactive non-login shells.

Share this:

  • Share on X (Opens in new window) X
  • Share on Facebook (Opens in new window) Facebook
  • More
  • Print (Opens in new window) Print
  • Email a link to a friend (Opens in new window) Email
  • Share on LinkedIn (Opens in new window) LinkedIn
  • Share on Reddit (Opens in new window) Reddit
  • Share on Tumblr (Opens in new window) Tumblr
  • Share on Pinterest (Opens in new window) Pinterest
  • Share on Telegram (Opens in new window) Telegram
  • Share on WhatsApp (Opens in new window) WhatsApp
  • Share on Mastodon (Opens in new window) Mastodon

Articles

How to install Cassandra 2 on CentOS 7 / RHEL 7
create and delete user in Red Hat and CentOS
Essential Linux Commands Every Beginner Should Know | With Examples
How to install Java (OpenJDK) on Ubuntu Linux
Bash Code Injection Vulnerability via Specially Crafted Environment Variables
How to convert rpm file into deb file
Yum command to download rpm file without installing in linux system
How To Install pip3 / pip2 In Linux
list files and directories by 4 commands in linux
Learn tar command and know about its precaution
How to install Jenkins on Ubuntu 18.04 LTS server
Find installed and enabled apache module in Debian/Ubuntu
How to install jboss application Server in CentOS 6.x
Install and set Java on Ubuntu 14.04 LTS with tar ball file
Extract single and selected files from RPM package on Linux

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

Reader Interactions

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

Articles

  • Install n8n docker in Ubuntu for practicing
  • Bash Script: MySQL Database backup to AWS S3
  • Automate no touch Jenkins setup wizard on docker (jcac)
  • How to run and configure Jenkins in Docker container
  • Complete Guide: How to install Nagios Monitoring on AlmaLinux / Rocky Linux
  • Install KVM on Ubuntu 24.04 LTS ( Bash Script )
  • How to install GIMP 2.10 on Ubuntu / Debian
  • 5 ways to check Ubuntu version of system (commands and GUI)
  • How To Solve Forgot MySQL / MariaDB Root Password
  • Create Self Hosted Build Agent Docker Image for Azure DevOps

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

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