How to set JAVA_HOME properly?
Before showing you how to set JAVA_HOME on all the operation systems, let's explain what is an environment variable? And what is the use of JAVA_HOME environment variable?
Table of Contents
As a prerequisite you will need to have a installed JDK or JRE on your machine. See for details in article Install JDK aka Java SDK
As you probably know, almost all java code has a variable that stores a value which is used many times in a program. An environment variable is like a final variable (in java) but for your environment. So, JAVA_HOME is a environment variable used to point to the directory where JRE or JDK is installed in your environment.
Let's go right to the point. So how to set JAVA_HOME on Windows, Mac os, and Linux?
1. Set JAVA_HOME on Windows
There are two ways to configure JAVA_HOME on windows depending on which version you have (windows 10, 8 or 7). In order to set JAVA_HOME environment variable, please follow below steps.
1.1. Set JAVA_HOME on Windows 10 and 8
To set JAVA_HOME on windows 10/8, you need to follow these steps below:
- Search for "advanced system settings" on windows Search
- Then, select "View advanced system settings" in the shown options
- Now you need to click on "Environment Variables" under the Advanced tab
- Click on "New (or User variables for single user)" in the System variables section
- In this step, you will have to set JAVA_HOME as the Variable name and in the Variable value you will have to give the path to JDK installation, then click OK
- To finish the configuration, click OK then Apply to save changes
1.2. Windows 7
Configuring the environment variable on windows 7 is little bit different. Below the steps.
- On the Start menu or on the desktop, right-click on "My computer" and then select "Properties"
- Click on "Environment Variables" under the Advanced tab
- Click on "New (or User variables for single user)" in the System variables section
- Now, set JAVA_HOME as the Variable name and as the Variable value, the path to JDK installation, then click OK
- Click OK then Apply to save and apply changes
1.3. Verifying JAVA_HOME configuration
To verify that you correctly set the JAVA_HOME environment variable, open Command prompt (windows + R) and check JAVA_HOME variable's value:
echo %JAVA_HOME%
If you have rightly configure JAVA_HOME, the result should be similar to the following:
C:\Program Files\JAva\jdk-12
2. Set JAVA_HOME on Mac OS X
Setting the JAVA_HOME environment variable on Mac OS is little bit different from configuring it on windows. To do so, all you need is to follow the steps described below.
2.1. Mac OS X 10.5 or Newer - Single user
Since OS X 10.5 was revealed, Apple added a command line tool (/usr/libexec/java_home) which dynamically finds for the current user, the top java version specified in Java Preferences.
Let's start by opening ~/.bash_profile in any text editor and add:
export JAVA_HOME=$(/usr/libexec/java_home)
Then, save and close the file.
On the Terminal, run the following source command to apply all the changes.
source ~/.bash_profile
At this point, you can check JAVA_HOME variable’s value using:
echo $JAVA_HOME
Below the expected result (jdk installation path)
/Library/Java/JavaVirtualMachines/jdk1.8.0_111.jdk/Contents/Home
2.2. Mac OS X Older Versions
Before OS X 10.5, you had to set the exact path to the JDK installation. If you have the concerned version of Mac Os, you need to to open ~/.bash_profile in any editor and add:
export JAVA_HOME=/path/to/java_installation
Then, save and close the file.
On the Terminal, run the following source command to apply all the changes.
source ~/.bash_profile
At this point, you can check JAVA_HOME variable’s value using:
echo $JAVA_HOME
Below the expected result (jdk installation path)
/Library/Java/JavaVirtualMachines/jdk1.8.0_111.jdk/Contents/Home
2.3. Global Setting
Instead of setting JAVA_HOME for a single user, you can configure it globally by following the same steps as for single user except that you will have to use the file /etc/profile.
3. Set JAVA_HOME on Linux
On linux, there are two ways to set the JAVA_HOME environment variable. Below steps for configuring for both single user or globally.
3.1. Single User
To configure JAVA_HOME for a single user in Linux, you need to use /etc/profile or /etc/environment (system-wide setting or ~/.bashrc (user specific setting).
Now you need to open ~/.bashrc in your text editor using:
vi ~/.bashrc
and add:
export JAVA_HOME=/path/to/java_installation
To apply changes, save and close the file.
Next step, you will have to run the source command to load the variable using the following:
source ~/.bashrc
To verify the JAVA_Home value, use:
echo $JAVA_HOME
And the result should be something similar to the following (the path of JDK installation):
/usr/lib/jvm/java-8-oracle
3.2. Global Setting
I personally prefer to set the JAVA_HOME globally for all users using:
/etc/profile
or
/etc/environment
So you have to open /etc/environment in any text editor using:
vi /etc/environment
and add:
JAVA_HOME=/path/to/java_installation
NB: /etc/environment is not a script. It represents a list of assignment which are read at the time of login.
To set the environment variable JAVA_HOME, you need to open /etc/profile and add in it:
export JAVA_HOME=/path/to/java_installation
Then run the source command in order to load the variable using:
source /etc/profile
Of course, you need to check JAVA_HOME variable’s value at the end of the configuration using:
echo $JAVA_HOME
And the result should be something similar to (the path of the JDK installation):
/usr/lib/jvm/java-8-oracle
PS: Please note that these steps are also valid for Ubuntu OS. All that you need to verify, is to check where is java installed by:
which java
Then, follow the steps described above.
4. Conclusion
To summarize, we’ve covered in today’s article, the steps that you need to follow to configure JAVA_HOME environment variable correctly in your environment (windows, mac os, and linux).