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

Circular dependancy causes ClassLinkageError

    XMLWordPrintable

    Details

    • Subcomponent:
    • Resolved In Build:
      rc
    • CPU:
      x86
    • OS:
      windows_nt

      Description



      Name: pa48320 Date: 07/12/2002

      Create an abstract class that depends on subclasses. When you load a subclass using Class.forName(), you will get a ClassLinkageError. This is new in 1.4 (I only tried 1.4.1) and does not happen in 1.3.1_03.
      Create the following class:
      public class Main
      {
          public static void main(String [] args) {
              try {
                  Class cl = Class.forName("pkg.Item1");
              } catch (Throwable t) {
                  t.printStackTrace();
              }
              System.out.println("Done.");
          }
      }

      Create the following 3 classes and put them in a jar file and put that jar file in the extensions directory:
      package pkg;
      public abstract class Item
      {
          public Item getItem(int type) {
              if (type == 1) {
                  return new Item1();
              } else if (type == 2) {
                  return new Item2();
              } else {
                  return null;
              }
          }

          public abstract String myName();
      }
      package pkg;
      public class Item1 extends Item
      {
          public String myName() {
              return "Item 1";
          }
      }
      package pkg;
      public class Item2 extends Item
      {
          public String myName() {
              return "Item 2";
          }
      }


      Now execute the first class. If you execute -verbose:class you will see output like this:
      <whole bunch of class loads deleted>
      [Loaded pkg.Item]
      [Loaded pkg.Item1]
      [Loaded pkg.Item2]
      [Loaded pkg.Item1]
      [Loaded java.lang.ClassFormatError from C:\j2sdk1.4.1\jre\lib\rt.jar]
      java.lang.LinkageError: duplicate class definition: pkg/Item1
      [Loaded java.lang.StackTraceElement from C:\j2sdk1.4.1\jre\lib\rt.jar]
      at java.lang.ClassLoader.defineClass0(Native Method)
      at java.lang.ClassLoader.defineClass(ClassLoader.java:502)
      at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:123)
      at java.net.URLClassLoader.defineClass(URLClassLoader.java:250)
      at java.net.URLClassLoader.access$100(URLClassLoader.java:54)
      at java.net.URLClassLoader$1.run(URLClassLoader.java:193)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.net.URLClassLoader.findClass(URLClassLoader.java:186)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:299)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:292)
      at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:265)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:255)
      at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:315)
      at java.lang.Class.forName0(Native Method)
      at java.lang.Class.forName(Class.java:140)
      at Main.main(Main.java:5)
      Done.

      If you execute this in 1.3.1 you will see the following output:
      <whole bunch of class loads deleted>
      [Loaded pkg.Item]
      [Loaded pkg.Item1]
      [Loaded pkg.Item2]
      Done.
      ======================================================================

        Attachments

          Activity

            People

            Assignee:
            acorn Karen Kinnear (Inactive)
            Reporter:
            pallenba Peter Allenbach (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: