JDK-4236389 : Class.getPackage() results dependent on packaging
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.lang:reflect
  • Affected Version: 1.2.0
  • Priority: P4
  • Status: Closed
  • Resolution: Cannot Reproduce
  • OS: generic
  • CPU: generic
  • Submitted: 1999-05-07
  • Updated: 2012-09-28
  • Resolved: 2001-03-07

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) 

WORK AROUND Name: skT88420 Date: 05/07/99 In order to get around this issue, one must parse the class name (exactly as getPackage() does) to get the Package for the class. ======================================================================

EVALUATION I can reproduce this in 1.2, but not in anything after kestrel. May be a duplicate of 4256589 fixed in kestrel. michael.mccloskey@eng 2001-03-07