Showing posts with label path variable. Show all posts
Showing posts with label path variable. Show all posts

Saturday, July 12, 2014

Setting Path Environment Variable in Java


For the beginner one of the difficult and confusing task is to set path and classpath environment variables.
Path is nothing but setting up an environment for operating system. Path is a system variable used to tell to Operating System all locations of executable files. Operating System will look in this path variable for executable files.
Classpath is nothing but setting up an environment for java. Classpath is pure java variable, which is used to tell the Java Virtual Machine or the Java compiler where to look for user-defined classes and packages.

For example,assume that Java is installed in C:\Program Files\Java\jdk1.7.0_25\ and your code is in C:\Sample\example.java, then path and classpath should be set to like this:
classpath=C:\Sample
path= C:\Program Files\Java\jdk1.7.0_25\bin;
Or
path=%JAVA_HOME%\bin;
where JAVA_HOME is pointing to Home directory “C:\Program Files\Java\jdk1.7.0_25”, where jdk is installed

Let us see what exactly means by setting path variable
Whenever you try to compile a program using javac, JVM will search for javac in current directory where your program is residing, If it does not find javac and other executables in current directory then it will search in the directories where the path variable is set to. So setting path variable allows you to execute your program from any directory as location of executables java and javac is retrieved through path variable.
If you have not set path variable then it will show error like “javac is not a recognized command” . hence to remove this error set path environment variable as follows:
  • Command to set path variable from command prompt

·         Set path=”C:\Program Files\Java\jdk1.7.0_25\bin”
·         Set path=%path%;”C:\Program Files\Java\jdk1.7.0_25\bin”
Here in second command we are just adding our path value to existing value of path variable.
  • Command to remove value from path variable

·         Set path=
This will remove directory path from environment variable.
  • Check whether environment variables are set correctly or not

·         If your environment variables are set correctly, then windows should recognize these commands.
C:\> java OR
C:\> javac
·         you can see current value of path variable by following command
C:\> echo %path%