Setting of JAVA_HOME Environment variable in Windows or Linux or Unix

Connect with

Oracle Java
JAVA_HOME is a system environment variable which basically represent JDK installation directory. When you install JDK in your system either windows, Linux or unix, it creates a home directory and puts all its binary i.e. under bin folder, library under lib folder and other utilities tools. After setting java_home you have to set

JAVA_HOME environment Variable in Windows

Step by Step process in Windows 7/8/10 as follows:

  • Right click your My Computer icon on your desktop and select Properties.
  • Click the Advanced tab.
  • Click the Environment Variables button.
  • Under System Variables, click New. ( you can set for logged-in user but preferebale is system variable so that any another user can get those variable)
  • Enter variable name as JAVA_HOME.
  • Enter variable value (one of the paths mentioned above) as the installation path for the JDK (Java Development Kit) before bin folder. e.g C:\JDK7\jdk1.7.0_65 ( I installed under JDK7 in side the c drive, I have changed this folder while installation)
  • Click OK button.
  • Click Apply Changes.

Once you set JAVA_HOME you have to set path as well if you want to run from command prompt.

JAVA_HOME environment Variable in Windows Temporarily

open commond prompt by typing cmd on run commnd.


set JAVA_HOME=C:\JDK7\jdk1.7.0_65

check by following command on command prompt.


echo %JAVA_HOME%

JAVA_HOME environment Variable in Unix/Linux

There are multiple place where you edit and put following line of

open the vi $HOME/.bashrc or vi $HOME/.bash_profile or vi /etc/profile

vim  ~/.bashrc

and place following line to set JAVA_HOME

export JAVA_HOME=/opt/JDK7/jdk1.7.0_65

Check it by following command on command prompt.

source ./profile
echo $JAVA_HOME

How to get JAVA_HOME environment in Java

While development sometimes if you have to get JAVA_HOME environemnt in java code, you can get by following way. it means, use java.home is Java system property and you can access it on java code using System.getProperty("java.home").

you can access java.home inside apache ANT as ${java.home}

Java program to get JAVA_HOME


package com.mysoftkey.core;

/**
 * This class is used to get JAVA_HOME environment variable in java.
 * 
 * @author ranjeet jha
 *
 */
public class JavaHomeExample {

  public static void main(String[] args) {
	String java_home = System.getProperty("java.home");
	System.out.println("JAVA_HOME : " + java_home);

  }

}

Output of Program

JAVA_HOME : C:\JDK7\jre7

Your comments/suggestions are welcome to improve this post.


Connect with

2 thoughts on “Setting of JAVA_HOME Environment variable in Windows or Linux or Unix

  1. Pingback: Kamla
  2. Pingback: BK

Leave a Reply

Your email address will not be published. Required fields are marked *