Tuesday, February 21, 2012

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 PATHand 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:

Windows 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.

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.

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.

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?