Setting of JAVA_HOME Environment variable in Windows or Linux or Unix

Connect with

JAVA_HOME Environment variable in Windows or  Linux or UnixJAVA_HOME environment variable referring to JDK installed directory before bin. Learn how to set JAVA_HOME Linux or JAVA_HOME Windows, get JAVA_HOME envrionment variable and set path.

1. JAVA_HOME environment Variable in Windows

When you install JDK in your system either Windows, Linux or UNIX, it creates a home directory and puts all its binary under bin folder, library under lib folder, and other utility tools. After setting JAVA_HOME and path environemnt variable $path=$JAVA_HOME/bin , you can access those utilty like javac or java etc from anywhere in your computer. You can How to install Oracle JDK 8 on Ubuntu , if you have not installed in your system.
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 environment variable you have to set path as well, if you want to run utility from command prompt from any location.

2. 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%

3. JAVA_HOME environment Variable in Unix/Linux

There are multiple place where you can 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

4. 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 env

package com.mysoftkey.core;

/**
 * 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 Java_home Program
Either you set JAVA_HOME Linux or JAVA_HOME Windows you can get environent variable using above piece of code.

JAVA_HOME : C:\JDK7\jre7

5. Reference

you can visit Java tutorial for more details
you can visit Oracle java for more details
Your comments/suggestions are welcome to improve this post of setting JAVA_HOME environment variable either in Linux or Windows.
Happy Learning! 🙂


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 Comment

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