In Java, the classpath is a parameter in the Java Virtual Machine or the Java compiler that specifies the location of user-defined classes and packages. It tells Java where to look in the filesystem for files defining these classes. The classpath can be set either on the command-line or through an environment variable. When executing Java programs, the Java Virtual Machine finds and loads classes lazily, meaning it loads the bytecode of a class only when the class is first used. The virtual machine searches for and loads classes in a specific order, which can be specified by the user.
To set the classpath, there are several options. The preferred way to specify the classpath is by using the -cp command line switch, which allows the CLASSPATH to be set individually for each application without affecting other applications. The -classpath option, when used to start the java application, overrides the CLASSPATH environment variable. If none are specified, the current working directory is used as the classpath. When overriding, it is advised to include the current folder "." into the classpath in the case when loading classes from the current folder is desired.
The CLASSPATH environment variable is used by the Java Virtual Machine to locate and load the .class files. It defines the path to find third-party and user-defined classes that are not extensions or part of the Java platform. The CLASSPATH depends on what you are setting the CLASSPATH for, and it has a directory name or file name at the end. The default value of CLASSPATH is a dot (.), which means only the current directory is searched. If CLASSPATH finds a class file that is present in the current directory, then it will load the class and use it, irrespective of the same name class present in another directory that is also included in the CLASSPATH. If you want to set multiple classpaths, then you need to separate each CLASSPATH by a semicolon (;).