How to Set JAVA_HOME on macOS
-
What is
JAVA_HOME
Environment Variable -
Set
JAVA_HOME
Environment Variable on macOS -
Global Setting of
JAVA_HOME
Environment Variable - Conclusion
JAVA_HOME
is an optional operating system environment variable set after installing the Java Runtime Environment (JRE
) or the Java Development Kit (JDK
). The JAVA_HOME
environment variable indicates the JDK or JRE installation location.
All operating systems, including Windows, Ubuntu, Linux, Mac, and Android, that include a Java installation must have this variable set. But here in this article, we will discuss how the JAVA_HOME
environment variable can be set on macOS.
The new method of defining the JAVA_HOME
environment variable enables us to dynamically recognize and employ the Java version currently installed on a system and if many Java versions are installed, to switch between them.
This article will give you straightforward steps to follow for setting the JAVA_HOME
environment variable on macOS.
What is JAVA_HOME
Environment Variable
The Java runtime installed locally does not use the JAVA_HOME
environment variable. Instead, other desktop software that needs the Java runtime will ask the JAVA_HOME
variable’s operating system to determine the runtime’s location.
Once the JDK
or JRE
installation’s location is identified, those applications can launch Java-based processes, launch Java virtual machines, and use command-line tools like the Java archive utility or the Java compiler, all of which are contained inside the bin
directory of the Java installation.
Set JAVA_HOME
Environment Variable on macOS
For setting up the JAVA_HOME
environment variable on macOS, first, you must identify the installed Java version of your macOS. There is a handy tool to determine the installed Java version of macOS 10.5 onward.
For instance, to print the most recent Java version installed on your Mac, open the terminal window and enter the following command.
/usr/libexec/java_home
This command dynamically locates the most recent Java version listed in the user’s Java Preferences.
Now, open ~/.bash_profile
and insert the following in any text editor:
export JAVA_HOME=$(/usr/libexec/java_home)
You can save this file using the ESC key on the keyboard and type :wq
. This will both save the file and end.
Save the document, then exit. In the terminal window type, immediately apply changes to the .profile
file.
source ~/.profile
In the terminal window, type:
java -version
It will print the current Java version installed on your Mac.
Once this step is complete, you are all left to run the command listed below, and you will be ready to use the JAVA_HOME
environment variable.
source ~/.bash_profile OR source ~/.zshrc
After executing the steps mentioned above, run the following command to ensure your computer’s JAVA_HOME
environment variable is configured correctly.
echo $JAVA_HOME
The command’s results should appear as in the instance below. This is the location of JDK’s installation.
/Library/Java/JavaVirtualMachines/jdk1.8.0_241.jdk/Contents/Home
JAVA_HOME
and macOS 11 Big Sur
zsh
is the default terminal shell on macOS 10.15 Catalina, and later, we can set the JAVA_HOME
environment variable in either /.zshenv
or /.zshrc
.
Open the ~/.zshenv
in the terminal and type:
% nano ~/.zshenv
Now, add the following lines in ~/.zshenv
.
export JAVA_HOME=$(/usr/libexec/java_home)
After that, source the file and print the JAVA_HOME
, and you are done.
In the terminal, type:
% source ~/.zshenv
% echo $JAVA_HOME
/Library/Java/JavaVirtualMachines/jdk-16.jdk/Contents/Home
JAVA_HOME
and Older macOS X
We must set JAVA_HOME
to the actual path because the function /usr/libexec/java_home
does not exist on earlier versions of macOS X.
Open the ~/.bash_profile
In the terminal, type:
% nano ~/.bash_profile
Insert the following lines in ~/.bash_profile
.
export JAVA_HOME=/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
Now, source the file and print the JAVA_HOME
.
% source ~/.bash_profile
% echo $JAVA_HOME
/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
Global Setting of JAVA_HOME
Environment Variable
The above commands will establish the JAVA_HOME
environment variable for a single user. But what would happen if you wanted this variable to be set globally for every user using the system?
The steps are the same for setting the JAVA_HOME
environment variable for a single user, but we employ the /etc/profile
or /etc/environment
file instead.
Open /etc/environment
and insert the following:
JAVA_HOME=/path/to/java_installation
Please be aware that the /etc/environment
is a collection of assignment expressions rather than a script (that is why export is not used). When logging in, it will read this file.
To set JAVA_HOME
using the /etc/profile
, here is what you will insert into the file:
export JAVA_HOME=/path/to/java_installation
To load the variable, execute the source command.
source /etc/profile
Now you can check the value of the JAVA_HOME
variable.
echo $JAVA_HOME
The result should be the path to the JDK installation.
/usr/lib/jvm/java-8-oracle
Conclusion
Every programmer should be able to set up the JAVA_HOME
environment variable before beginning any Java programming because many Java-based tools, apps, and libraries rely on it.
For example, to execute development tools like Eclipse, NetBeans, Tomcat, etc., you must adjust the operating system’s PATH
environment variable because these programs require JDK
or JRE
to function.
In this article, we have covered almost all the areas of how we can set the JAVA_HOME
environment variable for single users, and we also discussed the global setting for it. Following these simple steps, you will quickly set up your JAVA_HOME
environment variable on your macOS.
Zeeshan is a detail oriented software engineer that helps companies and individuals make their lives and easier with software solutions.
LinkedIn