13 Aug 2012

Setting the class path


A classpath is a path required by JVM to look for .class and other resources. Basically by setting the classpath you are telling the JVM  " hey , look for .class files
and other resouces like .jar or .zip files in this directory(s) ONLY". You can set more than one directory for a classpath. A default entry for a classpath is current directory which is represented by a period (.). .

I have Employee.class file in E:\Codes\ directory (Windows 7) . I'm going to set classpath to this directory and run the Employee.class file. I'll use this as an example in all of the methods described below.

Method #1:  jdk_tool -classpath path1;
For Windows, Linux and Solaris 
The jdktool are command line tools such as java , javac, javadoc or apt. One can use -classpath. or -cp (abbreviation form).
Open a commad prompt and issue the following command
>java -classpath E:\Codes Employee
This will tell the JVM to "set the classpath to directory to E:\Codes and run Employee.class file".
Note that if the Employee class would have been under a package, say org,  then fully qualified name of class must be specified.
>java -cp E:\Codes org.Employee

Points to remember
  • There must be a space after -classpath or -cp
  • Multiple path enries must be separated by semicolon (;)
  • This method will overrides the class-path entries defined by CLASSPATH and default value(which is a current directory).



CLASSPATH is a Envionment variable which can be set to a classpath name. There are two methods to set this variable.You can either use set CLASSPATH=path1; command or  System utitliy in Control Panel ( it depends on the operating system).


Method #2:  setting  CLASSPATH variable 
For Windows systems 
One can set the classpath explicity by issuing set CLASSPATH=path1;path2; command at the command prompt. Here the steps for that.
1.open command prompt .
2. type  >set CLASSPATH=E:\Codes
3. run the .Employee.class file using >java Employee.
note that once you closed the command prompt , the classpath gets cleared itself. Next time you have to set it explicitly 
You can unclear the classpath in case if classpath is not set correctly by using : 
>set CLASSPATH=
Points to remember
  • Multiple path entries must be separated by semicolon (;).
  • There should be no space around =.
  • The order in which these path are specified is important since JVM will search in that particular order.
  • The path must either be a file name (.class or .jar or .zip) or directry(which have the .class file(s)).
  • The value of the CLASSPATH environment variable, which overrides the default value (current directory). So if you want the current directory to be searched then you should use "."( period) .
For Linux and Solaris 
To set the classpath in csh shell , use following command
          >setenv CLASSPATH=path:
To set the classpath in csh shell , use following command 
                  >CLASSPATH=path:
                  >export  CLASSPATH

To unclear the classpath in csh shell , use following command
       >unsetenv CLASSPATH
To unclear the classpath in csh shell , use following command 
             >unset CLASSPATH


Method #3: System utility in Control Panel
For Windows XP
       control panel-->System-->Advanced tab
For Windows 7
control panel-->System--> Advanced system setting





For Linux and Solaris 
In csh shell , examine the .cshrc file for setenv command.
In sh shell ,  examine the .profile file for export command.


So many methods to set the classpath , which should one use ?
Method 2 is a recommended way to set the classpath. Because each application can have its own set of  classes to work without  interfering  with any other application.


No comments:

Post a Comment