Java Reference
In-Depth Information
Theonlywaytoeliminatethepreviouserrormessageistoloadaclassfromthepack-
age. Accomplish this task by merging the following code fragment into
Listing 4-3
.
if (args.length == 3)
try
{
Class.forName(args[2]);
}
catch (ClassNotFoundException cnfe)
{
System.err.println("cannot load "+args[2]);
return;
}
This code fragment, which must precede
Package pkg = Pack-
age.getPackage(args[0]);
, loads the classfile named by the revised
Pack-
ageInfo
application's third command-line argument. (I'll discuss
Class.forName()
later in this chapter.)
Run the new
PackageInfo
application via
java PackageInfo logging
1.5 logging.File
andyouwillobservethefollowingoutput—thiscommandline
identifies
logging
's
File
class as the class to load:
Name: logging
Implementation title: null
Implementation vendor: null
Implementation version: null
Specification title: null
Specification vendor: null
Specification version: null
Sealed: false
Exception in thread "main" java.lang.NumberFormatException:
Empty version string
at
java.lang.Package.isCompatibleWith(Package.java:228)
at PackageInfo.main(PackageInfo.java:42)
Itisnotsurprisingtoseeallofthese
null
valuesbecausenopackageinformationhas
been added to the
logging
package. Also,
NumberFormatException
is thrown