Manual setting

There are two ways to set it:

We will describe how to use the -classpath option first. It has the same format in the javac and java commands:

-classpath dir1;dir2\*;dir3\alibrary.jar  (for Windows)

javac -classpath dir1:dir2/*:dir3/alibrary.jar (for Lunix)

In the preceding example, dir1, dir2, and dir3 are folders that contain the files of the application and the third-party .jar files the application depends on. Each can include a path to the directory, too. The path can be absolute or relative to the current location where you run this command.

If a folder does not contain .jar files (it has only .class files, for example), it is enough to have only the folder name listed. Both tools—javac and java—will look inside the folder when searching for a particular file. A dir1 folder provides such an example.

If a folder contains .jar files (with .class files inside), then you can do one of two things:

The CLASSPATH environment variable serves the same purpose as the -classpath command option. The format of the list of file locations, specified as a value of the CLASSPATH variable, is the same as the list set with the -classpath option described earlier.  If you use CLASSPATH, you can run the javac and java commands without the -classpath option. If you use both, then the value of CLASSPATH is ignored. 

To see the current value of the CLASSPATH variable, open a Command Prompt or Terminal and type echo %CLASSPATH% for Windows OS or echo $CLASSPATH for Linux. Chances are you will get back nothing, which means that the CLASSPATH variable is not used on your computer. You can assign a value to it using the set command.

It is possible to include the CLASSPATH value with the -classpath option:

-classpath %CLASSPATH%;dir1;dir2\*;dir3\alibrary.jar (for Windows)

-classpath $CLASSPATH:dir1:dir2/*:dir3/alibrary.jar (for Lunix)

Notice that the javac and java tools are part of JDK, so they know where to find the Java standard libraries coming with JDK, and there is no need to specify the standard libraries' .jar files on the classpath. 

An Oracle tutorial for how to set a classpath is provided at  https://docs.oracle.com/javase/tutorial/essential/environment/paths.html.