Java Reference
In-Depth Information
As I said, you can tell the compiler about the path to your package by using the -classpath option on
the command line. Assuming that the Geometry directory is a subdirectory of C:\Beg Java Stuff , you
could compile the Line.java source file with the command:
javac -classpath "C:\Beg Java Stuff" Line.java
The command must be executed with Geometry as the current directory. This results in both the
Line.java and Point.java files being compiled because Line.java refers to the other class. Because the
directory in the path contains spaces, you have to enclose the path string between double quotes.
If the Point and Line classes were not interrelated, you could still compile the two source files or, indeed,
any number of source files, in the Geometry package with the following command:
javac -classpath "C:\Beg Java Stuff" *.java
Accessing a Package
How you access a package when you are compiling a program that uses the package depends on where you
have put it. There are a couple of options here. The first, but not the best, is to leave the .class files for the
classes in the package in the directory with the package name.
Let's look at that before going on to the second possibility.
With the .class files in the original package directory, either the path to your package must appear in
the string that has been set for the CLASSPATH environment variable, or you must use the -classpath option
on the command line when you invoke the compiler or the interpreter. This overrides the CLASSPATH envir-
onment variable if it happens to be set. Note that it is up to you to make sure that the classes in your package
are in the right directory. Java does not prevent you from saving a file in a directory that is quite different
from that appearing in the package statement. Of the two options here, using the -classpath option on the
command line is preferable, because it sets the classpath transiently each time and can't interfere with any-
thing you do subsequently. In any event, you can explore both possibilities.
If you elect to use the CLASSPATH environment variable, it needs to contain only the paths to your pack-
ages. The standard packages that are supplied with Java do not need to be considered, as the compiler and
the interpreter can always find them.
Of course, you can have as many paths as you want defined in CLASSPATH . They just need to be separated
by semicolons under Windows. If you are using Windows 7 then you can create and set environment vari-
ables through the Advanced system settings option in the dialog that you can access by selecting System in
Control Panel.
Under Linux, the mechanism to set CLASSPATH depends on the shell you are using, so check the docu-
mentation for it.
If you are using the JDK, you can always specify where your packages can be found by using the -
classpath option when you execute the Java compiler or the interpreter. This has the advantage that it ap-
plies only for the current compilation or execution, so you can easily set it to suit each run. The command to
compile MyProgram.java defining the classpath to include C:\MySource and C:\MyPackages would be:
javac -classpath "C:\MySource;C:\MyPackages" MyProgram.java
The compiler will find files in the current directory without specifying a period in the classpath as long as
the files are not in a package. The directory containing the package directory must appear in the classpath. If
Search WWH ::




Custom Search