Java Reference
In-Depth Information
In other words, the name of the package affects where you store your byte-
code. Suppose, for example, a class named Vehicle is in the com.wiley.trans
package, which contains classes representing modes of transportation. The
fully qualified name of this class is:
com.wiley.trans.Vehicle
The corresponding bytecode for the Vehicle class is in a file named Vehicle
.class. Because Vehicle is in the com.wiley.trans package, Vehicle.class needs to
be in a directory named trans, which is a subdirectory of wiley, which is a sub-
directory of com.
The com directory can appear anywhere on your hard drive. For example, if
com is in a folder called c:\my_bytecode, the path for Vehicle.class would be:
c:\my_bytecode\com\wiley\trans\Vehicle.class
The folder c:\my_bytecode is arbitrary, but the \com\wiley\trans directory
structure must exist and contain the file Vehicle.class.
Java is case sensitive. Because the package name com.wiley.trans is all
lowercase, the directory names need to be all lowercase, even though
directory names in Windows and DOS are not case sensitive.
The necessary file structure can be created in two ways:
You can manually create the directories and save your *.java files in the
same folder as your *.class files. The *.java files can then be compiled in
the usual manner, with the *.class being generated in the same directory
as the *.java file.
■■
You can use the -d flag of the javac compiler, specifying an output
directory where you want your bytecode to go. If the required package
directory structure does not exist, the -d flag will create the directories
and put the *.class file in the appropriate folder.
■■
Both options have their advantages. The first option is convenient in terms
of managing your source code on your hard drive, plus the files are in the
required format for using the javadoc tool (discussed later in this chapter). The
second option is convenient if you want to separate your bytecode from your
source code, a common need in Java when deploying applications (because
the bytecode is all that is needed).
Whichever method you choose, the process of creating the directories can be
confusing and frustrating, especially when classes no longer can find each
other; therefore, I want to go through an example so that you can see how the
process works first hand. Open your text editor, and follow along with me
through the following steps.
Search WWH ::




Custom Search