-
Bug
-
Resolution: Not an Issue
-
P2
-
None
-
1.2.0
-
x86
-
generic
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
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
- duplicates
-
JDK-4165020 ClassFormatError:"Illegal constant pool type" in defineClass
- Closed