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

ClassFormatError: Applet (Illegal UTF8 string in constant pool) in defineClass

    Details

    • Subcomponent:
    • CPU:
      x86
    • OS:
      generic

      Description

      The code below supposed to load an applet from a URL and define it. The example below uses the Animator class from the /usr/local/java jsut to demonstrate the bug. Everything goes smoothly until defineClass() is called and fails ( tried on Solaris 2.5.1 with JDK1.2beta4) Exception in thread "main" java.lang.ClassFormatError: Applet (Illegal UTF8 string in constant pool)


      import java.util.*;
      import java.net.*;
      import java.io.*;
      import java.applet.*;
      public class FarmletTender {
          public FarmletTender () {
          }
          static public void main(String args[]) {
              try {
                  FarmletLoader loader = new FarmletLoader();
               Applet farmlet =
                 (Applet)loader.loadClass("Applet", true).newInstance();
                  System.out.println("Hello World");
                  Thread.sleep(10000);
              } catch (Exception e) {
                  System.out.println("Got exception: " + e);
              }
          }
      }
      class FarmletLoader extends ClassLoader {
          //String host;
          //int port;
          Hashtable cache = new Hashtable();

      FarmletLoader() {
      super();
      }

          private byte loadClassData(String name)[] throws IOException {
              // load the class data from the connection
              URL url =
             new URL("file:/usr/local/java/jdk1.1.6/solaris/demo/Animator/1.1/Animator.class");
              System.out.println("url is: " + url);
              URLConnection link = url.openConnection();
              System.out.println("length is: " + link.getContentLength());
              System.out.println("type is: " + link.getContentType());
              System.out.println("encoding is " + link.getContentEncoding());
              System.out.println("content object is: " + link.getContent());
              byte data[] = new byte[link.getContentLength()];
              link.getInputStream().read(data, 0, link.getContentLength());
              System.out.println(" content is: " + data[0] + data[1] + data[2]);
              return data;
          }
          public synchronized Class loadClass(String name,
                                              boolean resolve)
            throws ClassNotFoundException {
              try {
                  Class c = (Class)cache.get(name);
                  if (c == null) {
                      byte data[] = loadClassData(name);
                      System.out.println("length = " + data.length);
                      c = defineClass(name, data, 0, data.length);
                      System.out.println("here " + c);
                      cache.put(name, c);
                      System.out.println("here");
                  }
                  if (resolve)
                      resolveClass(c);
                          System.out.println("here");
                  return c;
              } catch (Exception e) {
                  throw new ClassNotFoundException("Error loading class " + name
      +
                                    ": " + e);
              }
          }
      }
      The output as below:

      javac FarmletTender.java
      java FarmletTender
      url is: file:/usr/local/java/jdk1.1.6/solaris/demo/Animator/1.1/Animator.class
      length is: -1
      type is: application/java-vm
      encoding is null
      content object is: java.io.BufferedInputStream@34dd084d
       content is: -54-2-70
      length = 16323
      Exception in thread "main" java.lang.ClassFormatError: Applet (Illegal UTF8 string in constant pool)
              at java.lang.ClassLoader.defineClass0(Native Method)
              at java.lang.ClassLoader.defineClass(Compiled Code)
              at FarmletLoader.loadClass(Compiled Code)
              at FarmletTender.main(Compiled Code)

      Please note that the

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                mr Mark Reinhold
                Reporter:
                duke J. Duke (Inactive)
              • Votes:
                0 Vote for this issue
                Watchers:
                0 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved:
                  Imported:
                  Indexed: