CLASSPATH: CLASSPATH describes the location where the required “.class files” are available.
Then JVM uses CLASSPATH to locate required .class files.
We can set the CLASSPATH in the following 3
ways.
1. By using environment variable CLASSPATH (in WINDOWS),by updating .bashrc (in LINUX/UNIX).
This CLASSPATH will be preserved even after system restart aslo. Hence this is permanent way of setting CLASSPATH.
2. Temporary for a particular command prompt
level by using “set” command ( in Windows OS),
"export" command (in Linux/Unix OS).
C:\>set CLASSPATH = %CLASSPATH%;C:\ME
me# export CLASSPATH= ${CLASSPATH}:/home
·
Once
if you close the command prompt/terminal automatically this class path will be lost.
3. We can set the CLASSPATH for a
particular command level by using “–cp” (or) “–classpath” option
C:\>java -cp D:\ME WordCount
By using -cp or -classpath option is conveying to the JVM that the required classes are available in D:\ME
Once command is executed this classpath will be lost.
Examples:
C:\java Student (valid) but Runtime error :java.lang.NoSuchMethodError: main Exception in thread “main” as Student.java doesn't have main(String[] args) method)
C:\java College //Runtime error : NoClassDefFoundError: College
C:\java -cp D:\ME College (valid)
C:\java -cp D:\ME Student // Runtime error : NoClassDefFoundError: Student (here JVM checks Student.class inside D:\ME, as it couldn't found the Student.class then thrown error).
- If we are not setting CLASSPATH then the JVM always search in the current working directory for the required .class files.
- if we are setting CLASSPATH explicitly then JVM won't search in current working directory.It always search in the specified CLASSPATH only.
If you know anyone who has started learning Java, why not help them out! Just share this post with them. Thanks for studying today!...
No comments:
Post a Comment