Setting Path and Classpath Variable’s For Java in Win NT
For beginners in java, setting these two variables causes a lot of confusion. Here is my attempt to make life a bit simpler for them. Any suggestion for improving this page is most welcome.
PATH
: This
variable simply tells the SYSTEM where to find javac.exe & java.exe to
execute. ie when you say
C:\>
javac myClass.java OR
C:\>java myClass
How
does the system know where is the executable javac or java stored ?
So all you have to do is to inform the system where your javac.exe and java.exe
are kept(In fact all executable files that come with jdk, like javah, javadoc,
jar, appletviewer, rmic etc). In case of jdk packages they are always stored in
the bin folder, so if jdk was installed in C drive, in folder jdk1.3 then the
PATH variable must point to C:\jdk1.3\bin
ie
you must have
PATH=C:\jdk1.3\bin
How to set them will be shown in
a little while, I am just dealing with the definitions first.
CLASSPATH
: So
now the system knows where the executables are, but all execuatble files in bin
folder like javac.exe and java.exe too depend on a few files to compile and run
your codes.Primarily it needs tools.jar.
This jar stores all predefined classes made by sun to compile
and execute your code. Just as all executables for jdk are stored in bin
folders, similarly all helping jars are stored in lib folder at the same level
as the bin folder.
So
CLASSPATH must point to C:\jdk1.3\lib;
ie
you must have
Just by including lib folder
tools.jar is automatically included, however if you add other jars like mail.jar
or activation.jar(which will be needed in your experiments with JAVAMail API) in the lib folder you have to explicitly set them in the
CLASSPATH.
ie now the CLASSPATH will be
CLASSPATH=C:\jdk1.3\lib;C:\jdk1.3\lib\mail.jar;
C:\jdk1.3\lib\activation.jar;
And for packages if we want to include all
child folders, append just one more thing ”.;” at the end of
CLASSPATH as shown in red color below.
CLASSPATH=C:\jdk1.3\lib;C:\jdk1.3\lib\mail.jar; C:\jdk1.3\lib\activation.jar;.;
Now
having understood the two variables lets see how do we set them.
There
are several methods to do this,
Set for the current DOS PROMPT. Temporary
Set
through batch file.
Temporary
Set
for System in Environment. Permanent.
1)Set
for current DOS PROMPT Temporary Setting.
When you open a command prompt from START->PROGRAMS->Command Prompt you get a screen as shown below :
If
I have my jdk1.3 installed in C drive, in folder named jdk1.3
Now type these lines to set path and classpath
C:\>
set PATH=C:\jdk1.3\bin; PRESS ENTER.
And
C:\>
set CLASSPATH=C:\jdk1.3\lib; PRESS
ENTER.
So
now you will have this
However
this will cause all previous PATH and CLASSPATH settings to be erased for the
CURRENT DOS PROMPT WINDOW ONLY. And
when this window will be closed you will have to again set path and classpath ,
next time you want to compile or run your codes. Hence this is a temporary
setting method for path and classpath.
If
you want to retain the original(system default settings) Environment PATH and
CLASSPATH values ie assuming your system has already these settings,
PATH=C:\Oracle\Ora81\bin;
CLASSPATH=C:\Oracle\Ora81\lib;c:\mysql\xyz.jar;
And
you want to add (append to already existing values) your jdk PATH and CLASSPATH
For windows set it as
PATH=%PATH%;C:\jdk1.3\bin;
CLASSPATH=%CLASSPATH%;C:\jdk1.3\lib;
Here,
%PATH% is a variable name that stores the value C:\Oracle\Ora81\bin;
And
%CLASSPATH% is a variable name that stores the value C:\Oracle\Ora81\lib;c:\mysql\xyz.jar;
So
when you check what is the path value for the current DOS PROMPT window just
type
C:\>PATH and
press enter,
the console will show something like this
PATH=
C:\Oracle\Ora81\bin;C:\jdk1.3\bin;
And
to check classpath issue the next command
C:\>echo
%classpath% and
press enter,
this time console will show
CLASSPATH=C:\Oracle\Ora81\lib;c:\mysql\xyz.jar;C:\jdk1.3\lib;
This
done you can begin to compile and excute your codes now.
2)
SET THROUGH BATCH FILE (Temporary Action)
So you are a lazy bum who is not
interested to type set Path and set Classpath every time you open a command
prompt, and still not ready to make
changes in System Environment Settings What you can do is to make a simple batch
file which will set path and classpath for you. Open a notepad and type the same
lines as I have shown above for setting path and classpath, save the file as
setMyClasspath.bat (or whatever name you fancy… as long as it has a .bat
extension)
The batch file will look as shown here :
@echo
Off
echo Setting PATH AND CLASSPATH
echo
*******************************************
SET PATH=%PATH%;C:\jdk1.3\bin;
SET CLASSPATH=%CLASSPATH%;C:\jdk1.3\lib;
echo Setting PATH AND CLASSPATH DONE
echo *******************************************
echo Current Settings ARE
echo ...........................................
Path
echo -------------------------------------------
echo CLASSPATH= %classpath%
echo -------------------------------------------
echo start compiling or running your code now..... :-)
In
this batch file you can remove all the echo statements ( I put them for checking
whats happening where and when ……)
SET
PATH=%PATH%;C:\jdk1.3\bin;
SET CLASSPATH=%CLASSPATH%;C:\jdk1.3\lib;
So batch file has been made, now all you got to do is , open a Dos Prompt and
type
C:\> setMyClasspath
(if
the batch file is stored in C drive or change to whichever directory you created
it in)
This
time your PATH and CLASSPATH have been set, once again they are temporary ie,
once you close the Command Prompt Window, the extra settings are gone. And next
time you wish to use it open a new window and blah blah blah….. as before.
Sample
Batch File I created
Simple
isnt it !!!
I recommend it too because you are not fiddling with the System Environment
Variables and these settings do not clash with other programs , However if you
are so lazy that you don’t want to create even a batch file ……. See next
method .
3)Setting
In System Environment (Permanent Setting…. Till system crash and reformat)
There are many ways to reach the system environment, One way is to right click on my Computer at desktop and click on properties. If you didn’t comprehend that look in the image below
Click
on Properties will open up this screen
Correct
now click on Environment Tab to see the screen below
Well
Path already has something set to it, so just go to the end in Value
textbox, and write C:\jdk1.3\bin; as the last value(anywhere infact, you
can write it right in the beginning too, as long as the whole items in the list
are separated by a semi-colon ;) Press Set once you are through with PATH.
Similarly
find classpath variable in list of System Variables, if it is
present append your values as you do for path, if it is not present
then write CLASSPATH in textfield
named Variable and
write the value for classpath in Value textfield, it will something like
what I have captured in screen shown below
And
finally press Apply & You have a permanent setting for PATH and CLASSPATH.
Well
thats all you really need to do…….
Wishing you a sunny and bright java,
Mail your suggestions/ comments to me at gaurav007_2000@yahoo.com
*
And if you want to learn about Linux & Unix env, go to joonsingh by clicking here.
** Please report errors on this page, I never know when or where I goof-up......