Tuesday, February 21, 2017

How to set JAVA_HOME environment in Linux, Unix and Windows

JAVA_HOME is a system environment variable which represent JDK installation directory. When you install JDK in your machine (windows, Linux or unix) it creates a home directory and puts all its binary (bin), library(lib) and other
tools. In order to compile java program "javac" tool should be in your PATH and in order to get that in PATH
we use JAVA_HOME environment variable. Many tools like ANT and web servers like tomcat use JAVA_HOME to find java binaries. In this article we will see how to set JAVA_HOME environment variable in different operating system including Windows (windows 7, vista, xp) and Linux (Unix).

Setting JAVA_HOME on Windows environment:

How to set JAVA_HOME environment in Linux, Unix and WindowsWindows environment (windows 7, vista, xp) are GUI based operating system and you can edit PATH system variable from GUI. you need to define another variable called JAVA_HOME and set Java installation directory as value for JAVA_HOME variable.

here is step by step guide:

1) Windows + Pause -- open System property Window
2) Click Advanced --> Environment variables
3) Click New on System Variable section
4) Add Variable Name : JAVA_HOME and Variable value: C:\Program Files\Java\JDK1.6\
5) Add Java into PATH as : %JAVA_HOME%/bin

An important thing to note here is that on Windows environment (windows 7, vista, xp), you have two sections
user defined variables and system Variables. variable declared in one section is not visible in other section
so if you set JAVA_HOME on user variable section and update PATH on System variable section, you will not be
able to get value of JAVA_HOME and it will be blank. So be careful with this.

Setting JAVA_HOME on Unix or Linux

Setting JAVA_HOME on Unix or Linux environment is more easy and straight forward. you just need to export
JAVA_HOME variable and its value and than you can refer its value in any shell script. here is an example
of how to set JAVA_HOME on Unix and Linux:

1) export JAVA_HOME=/opt/java/jdk_1.6
2) export PATH= ${PATH}:{JAVA_HOME}/bin

Now Java is added on your system PATH by using JAVA_HOME System variable. you can also check How to set PATH for Java in Linux or Unix for more details on step and explanation.

JAVA_HOME vs java.home

I have seen Java programmer confusing over JAVA_HOME and java.home, where former represent JDK installation
directory later represent JRE installation directory and you know Difference between JDK and JRE. JRE just contain java binaries and doesn't contain other tools which comes with JDK and required for Java development. java.home
is also a Java system property and you can access it on java code using System.getProperty("java.home"). 
Also all java system properties are exposed as ANT builtin property you can access java.home inside ANT as ${java.home}

That’s all on how to set JAVA_HOME in Windows and Linux. We have also seen difference between JAVA_HOME and java.home system property. Let us know if you come across any other difference between them.

Further Learning
Complete Java Masterclass
Java Fundamentals: The Java Language
Java In-Depth: Become a Complete Java Engineer!

Other Java Tutorials you may like


Aryasindhu Sahu said...

A small modification :

To set/export PATH for java-
export PATH= ${PATH}:${JAVA_HOME}/bin

check the '$' before {JAVA_HOME}, i.e. ${JAVA_HOME}.

Hillfenous said...

One of the main advantage of using JAVA_HOME variable to point Java installation directory is that :

1) It's easy to upgrade JDK without affecting your application startup and config file which points to JAVA_HOME. you just need to download new version and make sure your JAVA_HOME points to new version of Java. This is best benefit of using environment variable or links.

2) JAVA_HOME variable is short and concise instead of full path to JDK installation directory.

3) JAVA_HOME variable is platform independence i.e. if your startup script uses JAVA_HOME then it can run on Windows and UNIX without any modification, you just need to set JAVA_HOME on respective operating system.

4) JAVA_HOME is standard, which means other tools which needs Java e.g. Maven, Eclipse can refer this variable without having any knowledge of where Java is physically installed in your machine, which is obviously be different in different user's machine. JAVA_HOME allows everybody to access Java in a common and standard way.

Let me know if this make sense.

Prem said...

Does Apache Maven also uses $JAVA_HOME to find out Java version to build a project? I am also confused with PATH and JAVA_HOME environment variable, if both contains location of JDK binary, which will take preference?

Anonymous said...

does setting JAVA_HOME variable is mandatory to run Java programs?

Odair Devalier said...

work in windows 8.1???

Post a Comment