Java Reference
In-Depth Information
Suppose we have the following class defi nition:
package com.sybex.payroll;
public class Employee {
public Employee() {
System.out.println(
“Constructing a com.sybex.payroll.Employee”);
}
}
This
Employee
class is in the
com.sybex.payroll
package, so its compiled fi le
Employee
.class
must be in a directory with a pathname
\com\sybex\payroll
. This requires a
directory named
\com
, which can appear anywhere on your fi le system. Inside
\com
you
must have a
\sybex
subdirectory, which must contain a
\payroll
subdirectory.
The
\com
directory can appear anywhere on your fi le system. A common technique is
to put your source fi les in a directory named
\src
and your bytecode fi les in a directory
named
\build
. For example, suppose the
Employee
source fi le is in the following directory:
c:\myproject\src\com\sybex\payroll\Employee.java
Suppose you want the compiled code to be in the
c:\myproject\build
directory. You
can use the
-d
fl ag of the compiler to achieve this. The
-d
fl ag has two effects:
The compiled code will be output in the directory specified by the
-d
flag.
The appropriate directory structure that matches the package names of the classes is
created automatically in the output directory.
Consider the following compiler command, executed from the
c:\myproject\src
directory:
javac -d c:\myproject\build .\com\sybex\payroll\Employee.java
The
-d
fl ag specifi es the output directory as
c:\myproject\build
. Assuming the class
compiles successfully, the compiler creates the fi le
Employee.class
in the following
directory:
c:\myproject\build\com\sybex\payroll\Employee.class
Keep in mind the directory
c:\myproject\build
is arbitrary; we could have output the
bytecode into the directory of our choosing. After you start putting bytecode in arbitrary
directories on your fi le system, the compiler and the JVM need to know where to look to
fi nd it. They look for the bytecode fi les in your classpath, an important concept that the
next section discusses in detail.
Search WWH ::
Custom Search