Java Reference
In-Depth Information
42.
AClass b = new BClass(ac);
43.
// ac is given to BClass in the
44.
// constructor .
45.
}
46.
47. }
In the its -programs, there are examples of this. AClass is like StatusPanel
which provides a method to set the mouse coordinates. BClass is like the listener
that knows the coordinates and wants to set them in the status panel. The listener
gets a reference to a status panel in the constructor, so it can access the method
to set the coordinates.
B.5
The classpath
In Java, the classpath points to all directories containing class files to be used by
the javac and java commands. To specify a classpath one uses the -classpath
option of these commands. For an example, suppose our class files are located in
the directories 'D:
Java', and in the current working di-
rectory, which is denoted by a dot ( . ). Suppose the main file is called mymain.java .
In order that the compile command finds the files one uses
\
Java
\
Project', in 'C:
\
Stuff
\
javac -classpath .;D: \ Java \ Project;C: \ Stuff \ Java mymain.java
The paths are separated by semicolons and backslashes are used inside the
paths. This is correct for Windows systems. On UNIX/Linux systems, colons and
slashes are used instead. As a general remark, one should always include the
current working directory (dot) in the classpath. When using packages one should
!
include the directory one above the package in the classpath.
As mentioned in the introduction, Chapter 1, one should always run the javac
and java commands from the directory immediately above the root of the package
structure used. For the programs of this topic that means the directory containing
the directory its . The syntax then is (for Windows):
javac its \ [packageName] \ [sourceFileName].java
java its.[packageName].[sourceFileName]
Still it might happen that the compiler complains that it cannot find some class
files. This usually happens if an environment variable is set for the classpath.
These are variables that determine the classpath for the whole computer. They
might point to directories not containing the classes of the project. Then the
compiler looks in the wrong places. One can solve this problem either by including
the correct dictionaries into the environment variable or by specifying an explicit
class path in the javac and java commands. For the latter try
javac -classpath . its \ [packageName] \ [sourceFileName].java
java -classpath . its.[packageName].[sourceFileName]
Search WWH ::




Custom Search