Java Reference
In-Depth Information
test message #1
opening file x.txt
writing test message #2 to file x.txt
closing file x.txt
cannot connect to file-based logger
What happens when
logging
is moved to another location? For example, move
logging
totherootdirectoryandrun
TestLogger
.YouwillNowobserveanerror
messageabouttheJVMnotfindingthe
logging
packageandits
LoggerFactory
classfile.
You can solve this problem by specifying
-classpath
when running the
java
tool, or by adding the location of the
logging
package to the
CLASSPATH
envir-
onment variable. You'll probably find it more convenient to use the former option, as
demonstrated in the following Windows-specific command line:
java -classpath \;. TestLogger
ThebackslashrepresentstherootdirectoryinWindows.(Icouldhavespecifiedafor-
ward slash as an alternative.) Also, the period represents the current directory. If it is
missing, the JVM complains about not finding the
TestLogger
classfile.
Tip
IfyoudiscoveranerrormessagewheretheJVMreportsthatitcannotfindan
application classfile, try appending a period character to the classpath. Doing so will
probably fix the problem.
Packages and JAR Files
classfiles in JAR files, and is also used to extract a JAR file's classfiles. It probably
comes as no surprise that you can store packages in JAR files, which greatly simplify
the distribution of your package-based class libraries.
ToshowyouhoweasyitistostoreapackageinaJARfile,wewillcreatea
log-
ger.jar
filethatcontainsthe
logging
package'sfourclassfiles(
Logger.class
,
LoggerFactory.class
,
Console.class
, and
File.class
). Complete the
following steps to accomplish this task:
1. Makesurethatthecurrentdirectorycontainsthepreviouslycreated
logging
directory with its four classfiles.
2. Execute
jar cf logger.jar logging\*.class
.Youcouldalternat-
ivelyexecute
jar cf logger.jar logging/*.class
.(The
c
option