How to Set JAVA_HOME / PATH variable in Linux


 Set Environment Variable for single / All users:


Normally Based on our requirement we need to set the PATH variable in desired config files. Below is the config files and its desired usage.

File name                           - Where / when it need to use


/etc/profile                         -  Global for many shells
/etc/profile.d/myname.sh

/etc/bash.bashrc                 -  Global for bash only


~/.profile                            - local for many shells

~/.bashrc                           - local bash only




A. Set JAVA_HOME / PATH for a single user



$ vi ~/.bash_profile

export JAVA_HOME=/usr/java/jdkX_X_X/bin/java  //New Java version path

export PATH=$PATH:/usr/bin/:/add/new_java_path  //At the end Add the new path what you want.


Save and exit


Then run the below command to take effect

# source  ~/.bash_profile

   (or)

# . ~/.bash_profile



Verify the settings:

#echo $JAVA_HOME

#echo $PATH

#which java



B. Set JAVA_HOME / PATH for all users


You need to setup global config in /etc/profile OR /etc/bash.bashrc file for all users:


# vi /etc/profile

Next setup PATH / JAVA_HOME variables as follows:

export PATH=$PATH:/usr/java/jdk?.X.X.X/bin

export JAVA_HOME=/usr/local/jdk?.X.X.X/bin


Save and exit.

Then run the below commands

# source /etc/profile

   (or)

# . /etc/profile

Verify the settings:


#echo $JAVA_HOME

#echo $PATH

#which java

1 comments:

Post a Comment

Blogger Tips and TricksLatest Tips And TricksBlogger Tricks