Sabtu, 05 April 2014

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.





Other Java Tutorials you may like






























Source:http://javarevisited.blogspot.com/2012/02/how-to-set-javahome-environment-in.html

Tidak ada komentar:

Posting Komentar