Compiling to Another Directory – Building Blocks – 1Z0-829 Study Guide

Compiling to Another Directory

By default, the javac command places the compiled classes in the same directory as the source code. It also provides an option to place the class files into a different directory. The -d option specifies this target directory.

Java options are case sensitive. This means you cannot pass -D instead of -d.

If you are following along, delete the ClassA.class and ClassB.class files that were created in the previous section. Where do you think this command will create the file ClassA.class?

javac -d classes packagea/ClassA.java packageb/ClassB.java

The correct answer is in classes/packagea/ClassA.class. The package structure is preserved under the requested target directory. Figure 1.2 shows this new structure.

FIGURE 1. 2 Compiling with packages and directories

packagea

ClassA.java

packageb

ClassB.java

classes

packagea

ClassA.class

packageb

ClassB.class

To run the program, you specify the classpath so Java knows where to find the classes.

There are three options you can use. All three of these do the same thing:

java -cp classes packageb.ClassB

java -classpath classes packageb.ClassB java –class-path classes packageb.ClassB

Notice that the last one requires two dashes (–), while the first two require one dash (-­).

If you have the wrong number of dashes, the program will not run.

Three Classpath Options

You might wonder why there are three options for the classpath. The -cp option is the

short form. Developers frequently choose the short form because we are lazy typists. The -classpath and –class-path versions can be clearer to read but require more typing.

Table 1.2 and Table 1.3 review the options you need to know for the exam. There are many other options available! And in Chapter 12, “Modules,” you learn additional options specific to modules.

TABLE 1. 2 Important javac options

OptionDescription
-cp <classpath>Location of classes needed to compile the program
-classpath <classpath>
–class-path <classpath>
-d <dir>Directory in which to place generated class files
TABLE 1. 3 Important java options
OptionDescription
-cp <classpath>Location of classes needed to run the program
-classpath <classpath>
–class-path <classpath>

Compiling with JAR Files

Just like the classes directory in the previous example, you can also specify the location of the other files explicitly using a classpath. This technique is useful when the class files are located elsewhere or in special JAR files. A Java archive (JAR) file is like a ZIP file of mainly Java class files.

On Windows, you type the following:

java -­cp “.;C:\temp\someOtherLocation;c:\temp\myJar.jar” myPackage.MyClass

And on macOS/Linux, you type this:

java -­cp “.:/tmp/someOtherLocation:/tmp/myJar.jar” myPackage.MyClass

The period (.) indicates that you want to include the current directory in the classpath. The rest of the command says to look for loose class files (or packages) in someOtherLocation and within myJar.jar. Windows uses semicolons (;) to separate parts of the classpath; other operating systems use colons.

Just like when you’re compiling, you can use a wildcard (*) to match all the JARs in a directory. Here’s an example:

java -­cp “C:\temp\directoryWithJars\*” myPackage.MyClass

This command will add to the classpath all the JARs that are in directoryWithJars. It won’t include any JARs in the classpath that are in a subdirectory of directoryWithJars.