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

DefineClass() throws NoClassDefFoundError instead of ClassCircularityError

XMLWordPrintable

    • mantis
    • sparc
    • solaris_8
    • Verified



      Name: vrR10176 Date: 02/27/2002



      JNI function DefineClass() does not detect circularity if loaded class
      is its own superclass. In this case superclass can not be loaded and
      DefineClass() throws NoClassDefFoundError instead of
      ClassCircularityError.

      Java Native Interface Specification, section 4 - "JNI Functions"
      says about the function:

      "jclass DefineClass(JNIEnv *env, jobject loader, const jbyte *buf, jsize bufLen);

      Loads a class from a buffer of raw class data.
       ...
      THROWS:
      ...
      ClassCircularityError: if a class or interface would be its own
      superclass or superinterface.
      ...
      "

      This bug causes failure of new JCK-1.4a test:
      vm/jni/DefineClass/dfcl001/dfcl00103m1/dfcl00103m1.html

      To reproduce the issue execute the following test.
      Test tries to create class instance from a buffer of raw class data
      representing a class 'test' which is its own superclass.

      ------------ dfcltest.java -------------------------------

      public class dfcltest {

          public native Class nativeDefineCls();

          static {
              System.loadLibrary("dfcltest");
          }

          public static void main(String argv[]) {
              dfcltest tob = new dfcltest();
              try {
                  Class cl = tob.nativeDefineCls();
                  System.out.println("ClassCircularityError expected");
              } catch (ClassCircularityError e) {
                  System.out.println("Test passed " + e);
              } catch (Throwable e) {
                  System.out.println("Exception caught: " + e);
              }
          }
      }

      ------------ dfcltest.c ----------------------------------

      #include "jni.h"

      JNIEXPORT jclass JNICALL Java_dfcltest_nativeDefineCls (JNIEnv *env, jobject obj)
      {

      /* byte array contains the raw class file data of the class:
      class test extends test{
      }
      */

          unsigned char buf[34]= {
              0xca, 0xfe, 0xba, 0xbe, 0x00, 0x03, 0x00, 0x2d,
              0x00, 0x03, 0x07, 0x00, 0x02, 0x01, 0x00, 0x04,
              0x74, 0x65, 0x73, 0x74, 0x00, 0x01, 0x00, 0x01,
              0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
              0x00, 0x00
          };
          return (*env) -> DefineClass(env, "test", 0, (const jbyte*)buf, 34);

      }

      ------------ Logs ----------------------------------------
      % ls -1
      dfcltest.c
      dfcltest.java
      jni.h
      jni_md.h

      %javac -d . dfcltest.java
      %
      %cc -G -KPIC -o libdfcltest.so dfcltest.c
      %
      %java -version
      java version "1.4.0"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-b92)
      Java HotSpot(TM) Client VM (build 1.4.0-b92, mixed mode)
      %
      % java -Xfuture dfcltest
      Exception caught: java.lang.NoClassDefFoundError: test
      %

      ----------------------------------------------------------

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

            jloizeausunw Jane Loizeaux (Inactive)
            rslasunw Rsla Rsla (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: