JDK-6196068 : Class.forName throws NoClassDefFoundError on miscapitalized classname
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.lang:reflect
  • Affected Version: 5.0
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_2000
  • CPU: x86
  • Submitted: 2004-11-16
  • Updated: 2017-08-29
  • Resolved: 2017-08-29
Related Reports
Duplicate :  
Relates :  
Relates :  
Description
FULL PRODUCT VERSION :
java version "1.5.0-beta"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-beta-b32c)
Java HotSpot(TM) Client VM (build 1.5.0-beta-b32c, mixed mode)

Also found in:
java version "1.4.2_05"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_05-b04)
Java HotSpot(TM) Client VM (build 1.4.2_05-b04, mixed mode)

ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows 2000 [Version 5.00.2195]

A DESCRIPTION OF THE PROBLEM :
Class.forName(String) throws a NoClassDefFoundError when given an incorrectly-capitalized class name and when the class file is loaded from a case-insensitive filesystem.  Class.forName(String) should always throw a ClassNotFoundException in this case.

I suspect the problem occurs when a class file is found (usually because the filesystem is case-insensitive) but does not contain the expected class (because class names are case-sensitive).

The internal filesystems of Jar files are case-sensitive, so jar-based class loaders will not be subject to this bug -- and indeed, Class.forName("java.lang.class") correctly raises ClassNotFoundException (since java.lang.Class is stored in rt.jar, it won't come up as a match).

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Compile and run the test code on a Windows machine (but not within a jar file).

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Class.forName(String) should raise a ClassNotFoundException, since the class name does not correspond to an actual class.
ACTUAL -
Class.forName(String) raised a NoClassDefFoundError after it found a class file (in the current directory), but that file didn't contain a class with the given name.

ERROR MESSAGES/STACK TRACES THAT OCCUR :
java.lang.NoClassDefFoundError: classload (wrong name: ClassLoad)

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
public class ClassLoad {
  public static void main( String[] argv ) {
    try {
      Class.forName( "classload" );
    }
    catch (Throwable t) {
      System.err.println( t );
    }
  }
}
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
Pretend NoClassDefFoundError is equivalent to ClassNotFoundException.
###@###.### 2004-11-16 18:37:11 GMT