Friday, April 24, 2015

Difference between JAVA_HOME, CLASSPATH and PATH



JAVA_HOME: JAVA_HOME and JRE_HOME are not set by Java itself. These are useful for third party applications/programes (example: Maven, Apache Tomcat) expect one of these environment variables to be set to the installation directory of the JDK or JRE. If you are not using software that requires them, you do not need to set JAVA_HOME and JRE_HOME.

Differences between CLASSPATH and PATH
CLASSPATH
PATH
CLASSPATH is an environment variable which contains a list of directories and / or JAR files, which Java will look through when it searches for Java classes to load.
Path is an environment variable used by the operating system where it will look for native executable programs to run.
Classpath is nothing but setting up the environment for Java. Java will use to find compiled classes
PATH is nothing but setting up an environment for operating system. Operating System will look in this PATH for executables.
Classpath refers to the Developing Environment.
While Path refers to the operating system
You do not normally need to set the CLASSPATH environment variable. Instead of using this environment variable, you can use the -cp or -classpath option on the command line when using the javac and java commands.
You should add the bin subdirectory of your JDK installation (or bin directory of JRE) directory to the PATH, so that you can use the javac and java commands and other JDK tools (or JRE Tools) in a command prompt window.


How to set the path of java (JDK or JRE) temporary in Windows:
To setting up the temporary path of JDK (or JRE), you need to follow these steps:
  .                1. Open command prompt
  1. copy the path of jdk/bin directory
  2. write in command prompt: set Path=%Path%;C:\Program Files\Java\jdk1.6.0_43\bin

C:\Users\myName>javac Sample.java
'javac' is not recognized as an internal or external command,
operable program or batch file.
C:\Users\myName>set Path=%Path%;C:\Program Files\Java\jdk1.6.0_43\bin
C:\Users\myName>javac Sample.java
C:\Users\myName>java Sample
Hello World!
C:\Users\myName>



Setting Path of Java for Windows permanently:
  1. Right click on “My Computers” and open “Properties”
  2. In Windows Vista or Windows 7, go to "Advanced System Settings".  
  3. Go to “Advanced Tab” and click on Environment Variables button.
  4. Select 'Path' under the list of 'System Variables', and press Edit and add your Java bin path (i.e. C:\Program Files\Java\jdk1.6.0_43\bin or if you have only JRE installed in the system C:\Program Files\Java\jre6) after a semicolon
Note: JRE is for running the application or programs (i.e. end user requires only JRES). But for the developers it is required JDK.



Setting Java Path in Linux OS
Setting the path in Linux OS is same as setting the path in the Windows OS. But here we use export tool rather than set. Let's see how to set path in Linux OS:

export PATH=$PATH:/home/ jdk1.6.0_43/bin/

Here, we have installed the JDK in the home directory under Root (/home). 
....

If you would like to know more about Path environment variable please continue to read below section:
More info for Path environment variable:
Whenever you type something into a command prompt, if it is not a system command, it will searches in that directory that you have already added in the PATH variable.
C:\Users\MyName>notepad
It will open the notepad (here "notepad" is not a system command, and searches in the directory "path") This is because of “notepad.exe” file is in “C:\Windows\system32" location. And this path is already set in PATH variable.
C:\Users\mallik>path
PATH=C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;….
In command prompt, when you type “eclipse”
C:\Users\MyName>eclipse
'eclipse' is not recognized as an internal or external command, operable program or batch file.
C:\Users\MyName>

This is because of “eclipse” is neither system command nor its path is set into PATH environment variable. So if you want to run eclipse from here then you have to set its path into PATH variable.
After setting eclipse path into PATH environment variable, you can run eclipse from your command prompt at any location.
C:\Users\MyName>set Path=%Path%;C:\software\eclipse-jee-juno-SR2-win32-x86_64\eclipse
C:\Users\MyName>eclipse
C:\Users\MyName>

At this point Eclipse will open because we set in the Path.
After setting eclipse path into PATH environment variable, you can run eclipse from your command prompt at any location. This is temporary to this command prompt only, if you want to set permanently you have to set in the path under system environment variable as explained in the above section "Setting Path of Java for Windows permanently"

More info for Classpath variable: CLASSPATH is used by the JVM that tells the JVM where to find the class libraries, including user-defined classes and libraries to import or interpret at compile time or run time.
The CLASSPATH is a list of locations, where the JVM will search for classes (jars and directories).
If the CLASSPATH is set as
Environment Variable Name = “CLASSPATH”
Environment Variable Value = “D:\classesslocation;D:\jarslocation\sample.jar”
JVM will search Classes in “D:\classesslocation” or in “D:\jarslocation\ sample.jar”

No comments:

Post a Comment