Setting the JAVA_HOME

Setting the JAVA_HOME in Windows

You must follow these steps to set JAVA_HOME variable on your Windows system.
Right-click the My Computer icon on your desktop and select Properties.
Click Advanced system settings link.
Click the Advanced tab.
Click the Environment Variables button.
Under System Variables, click New.
Enter the variable name as JAVA_HOME
Enter the variable value as the installation path for the Java Development Kit as something like this C:\Program Files\Java\jdk1.8.0_51
1
Click OK.
Under System Variables, select Path system variable
Click Edit.
Scroll to the beginning of the Variable value and insert %JAVA_HOME%\bin;
2
Click OK.
3
Click OK.
Click Apply.
If the changes do not take effect restart Windows.

Setting the JAVA_HOME in Linux

  1. Launch Terminal by pressing Ctrl+Alt+T on your keyboard.
  2.  Enter the following command:
    $ gksudo gedit /etc/environment
  3. Depending on where you installed your Java, you will need to provide the full path. For this example, I installed Oracle JDK 7 in the /usr/lib/jvm/java-7-oracle directory.
    Scroll to the end of the file and enter the following:
    JAVA_HOME=/usr/lib/jvm/java-7-oracle
    export JAVA_HOME
  4. Save your file and exit gedit.
  5. Lastly, reload the system PATH with the following command:
    $ . /etc/environment
    The above method will save you the hassle in having to run the commands every time you log in to your computer.

Setting the JAVA_HOME in Mac

1. Mac OSX 10.5 or later

In Mac OSX 10.5 or later, Apple recommends to set the $JAVA_HOME variable to /usr/libexec/java_home, just export $JAVA_HOME in file ~/. bash_profile or ~/.profile.

$ vim .bash_profile
export JAVA_HOME=$(/usr/libexec/java_home)
$ source .bash_profile
$ echo $JAVA_HOME
/Library/Java/JavaVirtualMachines/1.7.0.jdk/Contents/Home

2. Older Mac OSX

For older Mac OSX, the /usr/libexec/java_home doesn’t exists, so, you should set JAVA_HOME to the fixed path :

$ vim .bash_profile
export JAVA_HOME=/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
$ source .bash_profile
$ echo $JAVA_HOME
/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home