Setting the classpath
In order for javac
to compile the code and for java
to execute it, they need to know the location of the files that compose the application. In Chapter 2, Java Language Basics, while explaining the format of the javac
and java
commands, we described how the -classpath
option allows you to list all of the classes and third-party libraries your application is using (or, in other words, depends on). Now, we will talk about setting this list.
Manual setting
There are two ways to set it:
- Via the
-classpath
command-line option - Via the
CLASSPATH
environment variable
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...