Use of CLASSPATH environment variable in Java!

CLASSPATH is the environment variable where Java compilers look for class files. Both javac & java programs will look into CLASSPATH environment variable to search for class files.

For eg: if your class files are located under c:\workspace folder, you need to add this folder path to CLASSPATH environment variable in-order to avoid using -cp switch in both javac & java.

CLASSPATH=c:\workspace

If you not set CLASSPATH, javac compiler & java interpreter will fail to compile & run the java programs. You have to explicitly provide the class files path through -cp switch to javac & java. Even though your class files are located in the same folder where java files exist, these programs will not look into the current folder until you set ‘.’ to the CLASSPATH environment variable.

If you use package statement in your java files, javac compiler & java interpreter looks into CLASSPATH environment variable to search for the package paths.

For eg: CLASSPATH is set to c:\workspace and your java files contains the package statement as

package com.codesteps.ws

javac & java looks for the class files under c:\workspace\com\codesteps\ws\ folder.

Leave a Reply