Читать книгу OCP Oracle Certified Professional Java SE 17 Developer Study Guide - Jeanne Boyarsky - Страница 81

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

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.

OCP Oracle Certified Professional Java SE 17 Developer Study Guide

Подняться наверх