PATH
and CLASSPATH
environment variables on Microsoft Windows, Solaris, and Linux:For Windows 7:
a) Setting PATH:
In Environment Variables --> System Variables --> Create New Variable with name PATH, if not exists.
Edit path as shown below:
Example:
Path: C:\Program Files\Java\jdk1.7.0\bin;
Check Sample Images:
b) Setting CLASSPATH:
In Environment Variables --> System Variables --> Create New Variable with name CLASSPATH, if not exists.
Edit CLASSPATH as shown below:
Example:
CLASSPATH: C:\Program Files\Java\jdk1.7.0\lib;
Check Sample Images:
Windows XP
- Select Start, select Control Panel. double click System, and select the Advanced tab.
- Click Environment Variables. In the section System Variables, find the
PATH
environment variable and select it. Click Edit. If thePATH
environment variable does not exist, clickNew
. - 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.
- From the desktop, right click the My Computer icon.
- Choose Properties from the context menu.
- Click the Advanced tab (Advanced system settings link in Vista).
- Click Environment Variables. In the section System Variables, find the
PATH
environment variable and select it. Click Edit. If thePATH
environment variable does not exist, clickNew
. - 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.
- From the desktop, right click the Computer icon.
- Choose Properties from the context menu.
- Click the Advanced system settings link.
- Click Environment Variables. In the section System Variables, find the
PATH
environment variable and select it. Click Edit. If thePATH
environment variable does not exist, clickNew
. - 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.
PATH Variable (Solaris and Linux):
You can run the JDK just fine without setting thePATH
variable, or you can optionally set it as a convenience. However, you should set the path variable if you want to be able to run the executables (javac
, java
, javadoc
, 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:% /usr/local/jdk1.7.0/bin/javac ClassName.java
% java -version
java
tool, if it can find it. If the version is old or you get the error java: Command not found, then the path is not properly set.To set the path permanently, set the path in your startup file.
For C shell (
csh
), edit the startup file (~/.cshrc
):set path=(/usr/local/jdk1.7.0/bin )
bash
, edit the startup file (~/.bashrc
):PATH=/usr/local/jdk1.7.0/bin: export PATH
ksh
, the startup file is named by the environment variable, ENV
. To set the path:PATH=/usr/local/jdk1.7.0/bin: export PATH
sh
, edit the profile file (~/.profile
):PATH=/usr/local/jdk1.7.0/bin: export PATH
java
command:For C shell (
csh
):% source ~/.cshrc % java -version
ksh
, bash
, or sh
:% . /.profile % java -version
CLASSPATH variable (All platforms)
TheCLASSPATH
variable is one way to tell applications, including the JDK tools, where to look for user classes. (Classes that are part of the JRE, JDK platform, and extensions should be defined through other means, such as the bootstrap class path or the extensions directory.)The preferred way to specify the class path is by using the
-cp
command line switch. This allows the CLASSPATH
to be set individually for each application without affecting other applications. Setting the CLASSPATH
can be tricky and should be performed with care.The default value of the class path is ".", meaning that only the current directory is searched. Specifying either the CLASSPATH variable or the
-cp
command line switch overrides this value.To check whether
CLASSPATH
is set on Microsoft Windows NT/2000/XP, execute the following:C:> echo %CLASSPATH%
% echo $CLASSPATH
If
CLASSPATH
is not set you will get a CLASSPATH: Undefined variable error (Solaris or Linux) or simply %CLASSPATH% (Microsoft Windows NT/2000/XP).To modify the
CLASSPATH
, use the same procedure you used for the PATH
variable.Class path wildcards allow you to include an entire directory of
.jar
files in the class path without explicitly naming them individually. For more information, including an explanation of class path wildcards, and a detailed description on how to clean up the CLASSPATH
environment variable.