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 example, 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 environment variable, 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 programs. 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. That is the importance of using this 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 example, 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.

Add a Comment

Your email address will not be published.