Java Reference
In-Depth Information
Runtime Search
WhenthecompileroranyotherJavaapplicationruns,theJVMwillencountertypesand
mustloadtheirassociatedclassfilesviaspecialcodeknownasa
classloader
(discussed
inAppendixC).TheJVMwillusethepreviouslystoredpackageinformationthatisas-
sociated with the encountered type in a search for that type's classfile.
TheJVMsearchestheJavaplatformpackages,followedbyextensionpackages,fol-
lowed by the user classpath (in left-to-right order) for the first classfile that contains
thetype.Ifnouserclasspathispresent,thecurrentdirectoryissearched.Ifnopackage
matches or the type cannot be found, a “no class definition found” error is reported.
Otherwise, the classfile is loaded into memory.
Note
Whetheryouusethe
-classpath
optionorthe
CLASSPATH
environment
variable to specify a user classpath, there is a specific format that must be followed.
Under Windows, this format is expressed as
path1;path2;...
, where
path1
,
path2
,andsoonarethelocationsofpackagedirectories.UnderUnixandLinux,this
format changes to
path1:path2:...
.
Playing with Packages
Suppose yourapplication needs to log messages to the console, to a file, orto another
destination.Itcanaccomplishthistaskwiththehelpofalogginglibrary.Myimplement-
ation of this library consists of an interface named
Logger
, an abstract class named
LoggerFactory
, and a pair of package-private classes named
Console
and
File
.
Note
ThelogginglibrarythatIpresentisanexampleofthe
Abstract Factory design
pattern
, which is presented on page 87 of
Design Patterns: Elements of Reusable
Object-Oriented Software
by Erich Gamma, Richard Helm, Ralph Johnson, and John
Vlissides (Addison-Wesley, 1995; ISBN: 0201633612).
sages.
Listing 3-17.
Describing objects that log messages via the
Logger
interface
package logging;
public interface Logger
{
boolean connect();