Java Reference
In-Depth Information
example, you could create a directory for the classes you create in this topic. Because
the title of this topic is Java Programming: From Problem Analysis to Program Design, you
could create a directory named
jpfpatpd
. You could then make subdirectories for
the classes used in each chapter, such as the subdirectory
Appendix
within the
directory
jpfpatpd
.
Suppose that you want to create a
package
to group the classes related to time. You
could call this
package
clockPackage
. To add the
class
Clock
to this package and to
place the package
clockPackage
within the subdirectory
Appendix
of the directory
jpfpatpd
, you include the following package statement with the file containing the
class
Clock
(Note that the
class
Clock
is the same as discussed in Chapter 8):
package
jpfpatpd.Appendix.clockPackage;
We put this statement before the definition of the
class
, like this:
package
jpfpatpd.Appendix.clockPackage;
public class
Clock
{
//put instance variables and methods here
}
The next step is to compile the file
Clock.java
using the compile command in the IDE
(integrated development environment) you are using.
The following discussion assumes that you have set the
Path
so that the files
javac.exe
and
java.exe
can be executed from within any subdirectory. Suppose that the file
Clock.java
is in the subdirectory
c:\jpfpatpd
. We assume that you have switched to
the subdirectory
c:\jpfpatpd
.
If you are using Java 7.0, which contains a command-line compiler, you include the
option
-d
to place the compiled code of the program
Clock.java
in a specific directory.
For example, the command:
javac -d c:\jre1.7.0\lib\classes Clock.java
places the compiled code of the program
Clock.java
in the subdirectory:
c:\jre1.7.0\lib\classes\jpfpatpd\Appendix\clockPackage
Similarly, the following command places the compiled code of the program
Clock.java
in the subdirectory
c:\jpfpatpd\Appendix\clockPackage
:
javac -d c:\ Clock.java
If the directories
jpfpatpd
,
Appendix
,and
clockPackage
do not exist, then the compiler
automatically creates these directories. Note that for the earlier command to execute
successfully, the subdirectory
c:\jre1.7.0\lib\classes
must exist. If this subdirectory
does not exist, you must first create it. Also, to be absolutely sure about the correct directory
path, check your system's documentation. Moreover, if you do not use the
-d
option with
the path of the subdirectory to specify the subdirectory in which to store the compiled code,
then the compiled code is, typically, stored in the current subdirectory.
Search WWH ::
Custom Search