Set JAVA_HOME on Windows 7, 8, 10, Mac OS X, Linux
1. Overview
2. Windows
. Open Search and type advanced system settings
. In the shown options, select the View advanced system settings link
. Under the Advanced tab, click Environment Variables
. In the System variables section, click New (or User variables for single user setting)
. Set JAVA_HOME as the Variable name and the path to the JDK installation as the Variable value and click OK
. Click OK and click Apply to apply the changes
2.2. Windows 7
. On the Desktop, right-click My Computer and select Properties
. Under the Advanced tab, click Environment Variables
. In the System variables section, click New (or User variables for single user setting)
. Set JAVA_HOME as the Variable name and the path to the JDK installation as the Variable value and click OK
. Click OK and click Apply to apply the changes
Open Command Prompt and check the value of the JAVA_HOME variable:
echo %JAVA_HOME%
The result should be the path to the JDK installation:
C:\Program Files\Java\jdk1.8.0_111
3. Mac OS X
From OS X 10.5, Apple introduced a https://developer.apple.com/library/content/qa/qa1170/index.html[command line tool] (/usr/libexec/java_home_) which dynamically finds the top Java version specified in Java Preferences for the current user.
Open ~/.bash_profile in any text editor and add:
export JAVA_HOME=$(/usr/libexec/java_home)
Save and close the file.
Open a Terminal and run the source command to apply the changes:
source ~/.bash_profile
Now we can check the value of the JAVA_HOME variable:
echo $JAVA_HOME
The result should be the path to the JDK installation:
/Library/Java/JavaVirtualMachines/jdk1.8.0_111.jdk/Contents/Home
3.2. Single User – Mac OS X Older Versions
Open ~/.bash_profile in any editor and add:
export JAVA_HOME=/path/to/java_installation
Save and close the file.
Open a Terminal and run the source command to apply the changes:
source ~/.bash_profile
Now we can check the value of the JAVA_HOME variable:
echo $JAVA_HOME
The result should be the path to the JDK installation:
/Library/Java/JavaVirtualMachines/jdk1.8.0_111.jdk/Contents/Home
4. Linux
We’re going to manipulate the PATH here, of course – so, if you haven’t done that before, here are the detailed instructions on how to do it.
4.1 Single User
To set JAVA_HOME in Linux for a single user, we can use /etc/profile or /etc/environment (preferred for system-wide setting) or ~/.bashrc (user-specific setting).
Open ~/.bashrc in any text editor and add:
export JAVA_HOME=/path/to/java_installation
Save and close the file.
Run the source command to load the variable:
source ~/.bashrc
Now we 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
4.2 Global Setting
Open /etc/environment in any text editor and add:
JAVA_HOME=/path/to/java_installation
Please note that /etc/environment is not a script, but a list of assignment expressions (that is why export is not used). This file is read at the time of login.
To set JAVA_HOME using /etc/profile, open the file and add:
export JAVA_HOME=/path/to/java_installation
Run the source command to load the variable:
source /etc/profile
Now we 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