Monday, 25 April 2016

How to set and override classpath for Java ?


Setting Classpath

CLASSPATH is an environment variable which is used by JVM to locate user defined classes.

To set classpath, specify value of environment variable CLASSPATH.
CLASSPATH variable is not case sensitive.
So, Classpath == CLASSPATH == classpath

Set in Windows
1. Right click on My computer - properties - Advanced - Environment variable.
2. Provide environment variable :
    Name : CLASSPATH
    Value : JAVA_HOME\lib;.
* include current directory by adding semicolon and dot sign

Check
Type in console :
echo %CLASSPATH%


Set in Unix / Linux
Put this line in either your .bash_profile or .bashrc script which will run on login in Linux / Unix Machine : export CLASSPATH="JAVA_HOME/lib:."
include current directory by adding colon and dot sign

Check

echo ${CLASSPATH}




Overriding Classpath while running programs
To override Java classpath defined by environment variable CLASSPATH , 
provide option "-cp" or "-classpath" while running your program

Example
CLASSPATH= /home/tester/classes
java -cp $CLASSPATH Test

No comments:

Post a Comment

Note: only a member of this blog may post a comment.