Wednesday, October 31, 2012

How to set JAVA_HOME and PATH in Linux and Windows

Java is a computer programming language that is concurrent, class-based, object-oriented, and specifically designed to have as few implementation dependencies as possible. It is intended to let application developers "write once, run anywhere" (WORA), meaning that code that runs on one platform does not need to be recompiled to run on another. Java applications are typically compiled to bytecode (class file) that can run on any Java virtual machine (JVM) regardless of computer architecture.
The original and reference implementation Java compilers, virtual machines, and class libraries were developed by Sun from 1991 and first released in 1995. 

It's been around 20 years and it still doesn't register JAVA_HOME and JAVA_PATH itself. So you end up like these error : 

So it feel like

JAVA_HOME environment variable is for to point to the directory where the Java runtime environment (JRE) is installed on your compute

Why PATH variable is important?
Set the PATH environment variable if you want to be able to conveniently run the executable (javac.exe, java.exe, javadoc.exe, and so on) from any directory without having to type the full path of the command. If you do not set the PATH variable, you need to specify the full path to the executable every time you run it, such as

The PATH environment variable is a series of directories separated by semicolons (;). Microsoft Windows looks for programs in the PATH directories in order, from left to right. You should have only one bin directory for the JDK in the path at a time (those following the first are ignored), so if one is already present, you can update that particular entry.
The following is an example of a PATH environment variable:

It is useful to set the PATH environment variable permanently so it will persist after rebooting

Windows System
For demo purpose, we assume 
JAVA_HOME  = C:\Java\jdk1.7.0
PATH  = C:\Java\jdk1.7.0\bin;

  • Depending on Window's version find and click Environment Variables. In the section System Variables, find the PATH environment variable and select it. Click Edit. If the PATH environment variable does not exist, click New. Similarly, for JAVA_HOME variable.
  • In the Edit System Variable (or New System Variable) window, specify the value of the PATH environment variable. Click OK. Close all remaining windows by clicking OK.
Note: You may see a PATH environment variable similar to the following when editing it from the Control Panel:

Linux System
For demo purpose, we assume 
JAVA_HOME  = /disk2/jdk1.7.0
PATH  =  /disk2/jdk1.7.0/bin

  • Launch the Terminal and sudo su to root.
  • Identify where is Java installed on your Linux Machine by typing
    Which java 
  • Now to set the JAVA_HOME globally, we edit the .bashrc or .bash_profile or .profile
    export JAVA_HOME
  • Now set the PATH
    export PATH
  • Finally execute the bash files depending on Linux version. For instance,
    . ~/.bash_profile
    . ~/.profile
  • Type java -version to check the correct Java version.

If you know anyone who has started learning Java, why not help them out! Just share this post with them. Thanks for studying today!...


  1. I am technology Enthusiast. Your blog is really awesome, attractive and impressive. I like the way you think. it is very useful for Java SE & Java EE Learners. Your article adds best knowledge to our Java Online Training in India. or learn thru Java Online Training in India Students. or learn thru JavaScript Online Training in India. Appreciating the persistence you put into your blog and detailed information you provide. Kindly keep blogging.