i

JAVA Complete Course

Understanding Classpath

CLASSPATH is an environment variable (i.e., global variables of the operating system available to all the processes) which is needed for the Java compiler and runtime in order to locate the packages/classes used in a Java program. It is similar to another environment variable PATH, which is used by the Command shell in order to find the executable programs.

CLASSPATH can be set in with the following ways:

  • CLASSPATH can be set constantly in the environment: In Windows operation system, steps are following: GO TO control panel ⇒ System ⇒ Advanced ⇒ Environment Variables ⇒ choose “System Variables” (for all the users) or “User Variables” (If we want only for the current logged in user) ⇒ choose “Edit” (in case if CLASSPATH already exists) or “New” ⇒ Enter “CLASSPATH” as the variable name ⇒ Enter the required directories and JAR files (separated by semicolons) as the value (e.g., “.;c:\myProject\classes;d:\tomcat\lib\servlet-api.jar”). Please note that we need to include the current working directory (denoted by ‘.’) in the CLASSPATH.

  • CLASSPATH can be set for short-term for that particular CMD shell session by running the following command

  • Instead of using the CLASSPATH environment variable, we can also use the command-line option –classpath (or –cp) of the javac and java commands