Set $JAVA_HOME in Linux
Software Engineering Team Lead and Director of Cloudsure
$JAVA_HOME an environment variable. It is the root path of the JRE or JDK which some applications and services use to access Java.
I know that Java is installed java -version
but its not in the path
echo $JAVA_HOME
, the output is empty.
Configure for single user
-
Open the shell script in your favorite editor eg.
vim ~/.zshrc
vim ~/.bashrc
-
Add this line to export the path the the corresponding directory
export JAVA_HOME=/usr/bin/java
which can be determined usingwhich java
if you are unsure. -
Add the path to $PATH
export $PATH=$PATH:$JAVA_HOME
-
Save the file.
-
Source the file in your terminal eg.
source ~/.zshrc
orsource ~/.bashrc
-
Execute
echo $JAVA_HOME
to verify the output.
Configure for all users
-
Login a root or use
sudo
to execute commands. -
Execute bash
sudo bash
-
Edit
vim /etc/bashrc
orvim /etc/profile
-
Add
export JAVA_HOME=/usr/bin/java
-
Save the file.
-
Source
source /etc/bashrc
orsource /etc/profile
-
Echo
echo $JAVA_HOME
-
exit
to exit the sudo bash terminal.
References
- What is the purpose of .bashrc and how does it work? - UNIX StackExchange
- Setting JAVA_HOME - Atlassian Opsgenie
- How to Set JAVA_HOME / PATH variables Under Linux Bash Profile - nixCraft