Name: skT88420 Date: 05/07/99 The Class.getPackage() method returns null when no package object was created by the class loader of the class instance. It would be more intuitive if getPackage() always returned a Package when the class is defined as part of a Package, regardless of the deployment mechanism. This might require that getPackage() create a Package when one has not already been created by the class loader. For example, the following code works fine when it is loaded from a jar file, but throws a null pointer exception when it is loaded from a directory in the classpath: package com.lawson.java.util; class firstClass {} public class mainClass extends Object { public static void main(String[] args) { System.out.println("firstClass class name = " + firstClass.class.getName()); System.out.println("firstClass package = " + firstClass.class.getPackage().getName()); } Since the jar file was created by simply specifying the directory for the package (i.e., jar cf myJar.jar com\lawson\java\util) there is no specific information regarding the package in the jar manifest. Therefore it seems arbitrary that getPackage() returns null when the class is loaded from a directory and a valid Package when it is loaded from a jar. (Review ID: 57964) ======================================================================
|