The compiler cannot find the class B—that is, the file B.class containing the Java bytecode for the class B. In Figure 6.4b, we can see that it is in the package pkg under the directory bin, but the compiler cannot find it. This is hardly surprising, as there is no bytecode file for the class B in the current directory, which is the default value of the class path. The following command sets the value of the class path to be /top/bin, and compilation is successful (Figure 6.4c):
>
javac -classpath /top/bin -d ../bin A.java
It is very important to understand that when we want the JDK tool to search in a named package, it is the location or the root of the package that is specified; in other words, the class path indicates the directory that contains the first component of the fully qualified package name. In Figure 6.4c, the package pkg is contained under the directory whose absolute path is /top/bin. The following command will not work, as the directory /top/bin/pkg does not contain a package with the name pkg that has a class B:
>
javac -classpath /top/bin/pkg -d ../bin A.java
Also, the compiler is not using the class path to find the source file(s) that are specified in the command line. In the preceding command, the source file has the relative pathname ./A.java. Consequently, the compiler looks for the source file in the current directory. The class path is used to find the classes used by the class A, in this case, to find class B.
Given the file hierarchy in Figure 6.3, the following -classpath option sets the class path so that all packages (wizard.pandorasbox, wizard.pandorasbox.artifacts, wizard.spells) in Figure 6.3 will be searched, as all packages are located under the specified directory:
-classpath /pgjc/work
However, the following -classpath option will not help in finding any of the packages in Figure 6.3, as none of the packages are located under the specified directory:
>
java -classpath /pgjc/work/wizard pandorasbox.Clown
This command also illustrates an important point about package names: The fully qualified package name should not be split. The package name for the class wizard.pandorasbox.Clown is wizard.pandorasbox, and must be specified fully. The following command will search all packages in Figure 6.3 for classes that are used by the class wizard.pandorasbox.Clown:
>
java -classpath /pgjc/work wizard.pandorasbox.Clown
The class path can specify several entries (i.e., several locations), and the JDK tool searches them in the order they are specified, from left to right.
-classpath /pgjc/work:/top/bin:.
We have used the path-separator character ‘:’ for Unix-based platforms to separate the entries, and also included the current directory (.) as an entry. There should be no whitespace on either side of the path-separator character. On the Windows platform, the path-separator character is a semicolon (;).
The search in the class path entries stops once the required class file is found. Therefore, the order in which entries are specified can be significant. If a class B is found in a package pkg located under the directory /ext/lib1, and also in a package pkg located under the directory /ext/lib2, the order in which the entries are specified in the two -classpath options shown below is significant. They will result in the class pkg.B being found under /ext/lib1 and /ext/lib2, respectively.
-classpath /ext/lib1:/ext/lib2
-classpath /ext/lib2:/ext/lib1
The examples so far have used absolute pathnames for class path entries. We can, of course, use relative pathnames as well. If the current directory has the absolute pathname /pgjc/work in Figure 6.3, the following command will search the packages under the current directory:
>
java -classpath . wizard.pandorasbox.Clown
If the current directory has the absolute pathname /top/src in Figure 6.4, the following command will compile the file ./A.java:
>
javac -classpath ../bin -d ../bin A.java
If the name of an entry in the class path includes whitespace, the name should be double-quoted so that it will be interpreted correctly:
-classpath “../new bin”
Table 6.1 summarizes the commands and options to compile and execute non-modular code. The tool documentation from Oracle provides more details on how to use the JDK development tools.
Table 6.1 Compiling and Executing Non-Modular Java Code
Operation | Command |
Compiling non-modular code: | javac –class-path classpath -d directory sourceFiles javac -classpath classpath -d directory sourceFiles javac -cp classpath -d directory sourceFiles |
Executing non-modular code: | java –class-path classpath qualifiedClassName java -classpath classpath qualifiedClassName java -cp classpath qualifiedClassName |