Configuring JAVA_HOME in Windows, Linux , MAC OS

JAVA_HOME is environment variable which point to an installed JDK location. Many software requires that we set JAVA_HOME to point to the installation directory. In this post lets see how we can configure JAVA_HOME in difference OS.

First step is to ensure that Java is installed on your machine [ any os ]. To check open terminal / Console / Command Prompt and execute javac command. It will display help of javac is found else will display command not found [ Install JDK ]. Following is screenshot of javac command executed on Mac OS. Output will be same on all OS if JDK is available.

Output of javac command on terminal

Now let us set the JAVA_HOME environment variable in Windows OS.

In Windows OS, JDK is installed in C:\Program Files\ Java folder. It will contain all installed JDK and JRE.

Where Java is installed in Windows

Open System from control panel or right click on my computer icon and click on properties. From the System window Click on Advance System Setting option on left side. It will open new window of System properties. Click on Advance Tab and Environment Variables as shown in below screenshot.

Setting Environment Variables - Control Panel - System

In Environment Variables window, Click on New button under System Variable to set Java home for system wide. We can also set it for user specific also.

Environement Variables

In Windows, Enter JAVA_HOME as Variable name and Path of JDK in Variable value. Click on Ok.
Adding Variable name and Variable value

We are done, JAVA_HOME variable is set on windows operating System.

To set the JAVA_HOME in Ubuntu or linux mint system, first we need to find the installation path for Java. Open the terminal and execute command whereis javac it will display /usr/bin/javac which is path to executable but not the actual directory now run the command ls -l /usr/bin/java which will show where it points to [ as it is link ] execute the ls -l the path it shows and this way you will get the actual path of java installation directory. See following screenshot
Finding Java installation on Linux Base System [ debain based ]

 We have the path of java installation, copy the path. Open the .profile file in your home directory in vim / gedit or any other editor and add export JAVA_HOME=PATH at the end of file. See following screenshot :

Adding JAVA_HOME in .profile file

Save the file. Logout and login again. Open the terminal and run echo $JAVA_HOME command it will show the path as show in following screen shot.
Echoing $JAVA_HOME on Terminal Linux Base System

We have set the JAVA_HOME path. You can also set the JAVA_HOME path in .bashrc file. .profile is read during the login. .bashrc files have effect on terminal window only.

To set it system wide you can add it in /etc/.profile also.

You can also see video for how to set path in Ubuntu.

In MAC OS X mostly all installed files can be found in Library folder in root. Following screenshot shows the finder window

Locating Java installation directory on MAC OS in Finder

Alternatively and better way is to execute /usr/libexec/java_home command on terminal which will show the path of java. Open the .bash_profile file in vim or other editor as shown below

Using /usr/libexecv/java_home in MAC OS X to find Java

 Add export JAVA_HOME=$(/usr/libexec/java_home). Save the file. We can also type absolute path also.
Adding JAVA_HOME in .bash_profile in MAC OS X

In terminal run source .bash_profile command and run echo $JAVA_HOME it will display the path of Java.

Re Loading .bash_profile in MAC OS

All set for MAC OS X also.

In Mac OS older version where /usr/libexec/java_home is not available, in that case we need to set JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home

Java in MAC OS X Finder

Labels: , , ,