Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4144318

ClassFormatError instead of NoClassDefFoundError when classfile has wrong name.

    XMLWordPrintable

    Details

    • Subcomponent:
    • Resolved In Build:
      1.2fcs
    • CPU:
      sparc
    • OS:
      solaris_2.4
    • Verification:
      Verified

      Description



      Name: akC45999 Date: 06/01/98



      The Java VM spec chapter 5.3.1 Loading Using the Default Class Loader reads:
      ...
      5.This phase of loading must detect the following errors:
             If the purported representation is not in ClassFile format (§4.1,
             pass 1 of §4.9.1), or is not of a supported major or minor version
             (§4.1), loading throws a ClassFormatError.
             Otherwise, if the purported representation does not actually
             represent C, loading throws an instance of
             NoClassDefFoundError or an instance of one of its subclasses.

      But the attached test shows that ClassFormatError is thrown when "the purported
      representation does not actually represent C", that is, the name stored in the class file
      differs from the file name.

      --------------------------------- defaultLoader00301.java

      import java.io.PrintStream;

      public class defaultLoader00301 {

        public static int run(String argv[], PrintStream out) {

      try{
      Class.forName("BadClass");
      out.println("No exception ");
      return 2;
      } catch (NoClassDefFoundError e) {
      return 0;
      } catch (Throwable e) {
      out.println("Unexpected exception " + e);
      return 2;
      }
        }

        public static void main(String argv[]) {
      System.exit(run(argv, System.out) + 95/*STATUS_TEMP*/);
        }

      } // end class defaultLoader00301

      --------------------------------- BadClass.jcod
      class BadClass {
        0xCAFEBABE;
        3; // minor version
        45; // version
        [] { // Constant Pool
          ; // first element is empty
          class #7; // #1
          class #12; // #2
          Method #2 #4; // #3
          NameAndType #6 #5; // #4
          Utf8 "()V"; // #5
          Utf8 "<init>"; // #6
      // Utf8 "BadClass"; // #7 // right
          Utf8 "BadClassName"; // #7 // wrong
          Utf8 "BadClass.java"; // #8
          Utf8 "Code"; // #9
          Utf8 "I"; // #10
          Utf8 "aField"; // #11
          Utf8 "java/lang/Object"; // #12
        } // Constant Pool

        0x0020; // access
        #1;// this_cpx
        #2;// super_cpx

        [] { // Interfaces
        } // Interfaces

        [] { // fields
          { // Member
            0x0008; // access
            #11; // name_cpx
            #10; // sig_cpx
            [] { // Attributes
            } // Attributes
          } // Member
        } // fields

        [] { // methods
        } // methods

        [] { // Attributes
        } // Attributes
      } // end class BadClass
      ------------------------------------- runtest
      #!/bin/ksh

      javac defaultLoader00301.java
      jcoder BadClass.jcod
      java -verify defaultLoader00301
      echo res=$?
      ----------------------------------------
      Running the test:

      novo64% runtest
      Unexpected exception java.lang.ClassFormatError: BadClass (Wrong name: BadClassName)
      res=97
      novo64%


      ======================================================================

        Attachments

          Activity

            People

            Assignee:
            sliangsunw Sheng Liang (Inactive)
            Reporter:
            rfqsunw Rfq Rfq (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: