[2187] in java-interest
ClassLoader problems
daemon@ATHENA.MIT.EDU (Chanda Dharap)
Tue Sep 26 17:32:57 1995
From: chanda@PRPA.Philips.COM (Chanda Dharap)
To: java-interest@java.sun.com
Date: Tue, 26 Sep 1995 10:40:27 -0700 (PDT)
Hi,
I tried defining my own ClassLoader following the directions from various
people.
The testloader is simple, just loads class from InputStream.
In fact I just copied it from WWWClassLoader.java to try it out.
1)
Here is the code:
-----------------------------------------
class testloader extends ClassLoader {
String host;
int port;
Hashtable cache = new Hashtable();
InputStream is;
public testloader(InputStream ins) {
is = ins;
}
public Class loadClass(InputStream is) {
byte bytes[] = new byte[4096];
byte buffer[] = new byte[1024];
int n;
int totalbytes = 0;
while( (n = is.read(buffer,0,buffer.length)) >= 0) {
if(totalbytes + n >= bytes.length) {
byte newbytes[] = new byte[((bytes.length + n) * 3) /2 ];
System.arraycopy(bytes,0,newbytes,0,totalbytes);
bytes = newbytes;
}
System.arraycopy(buffer,0,bytes,totalbytes, n);
totalbytes += n;
}
return defineClass(bytes,0,totalbytes);
}
}
--------------------------------------------
2)
This compiles very well.
Then I added code to call it as follows:
ClassLoader loader = new testloader(myinputstream);
Object main= loader.loadClass(myInputStream).newInstance();
This was based on the documentation in the header of ClassLoader.java
However, here is what happened:
-----------------------------------------------
> javac loadtest.java
loadtest.java:154: class testloader is an abstract class. It
does not define java.lang.Class loadClass(java.lang.String, boolean)
from class java.lang.ClassLoader.
ClassLoader loader = new testloader(myinputstream);
^
loadtest.java:155: No method matching loadClass(java.io.InputStream)
found in class java.lang.ClassLoader.
Object main= loader.loadClass(myinputstream).newInstance();
^
2 errors
---------------------------------------------------
But "testloader" is not an abstract class. In fact did'nt I just
define it ? Whats so abstract about that !! I checked in
ClassLoader.java to make sure I had definitions for all abstract
classes. This was the only one.
3)
So far no one talks about implementing interfaces and such. After this
I read comments on "Secrets of the ClassLoader" which says that I
should define an interface loaded on the "system" side of the class
loader, and then cast the new object to that interface object and
invoke it using the interface. (Should be titled "Unsolved Mystries of
the ClassLoader" don't you think ?)
So I look at Firewall.java which uses WWWClassLoader .. thinking that
I can follow an example just as well as the next person..
(Hah ! Was I wrong or what !!!!)
Firewall.java calls something called getClassLoader() which is a
native implementation and obviously returns a classloader which is
then cast into WWWClassLoader.
I'm kinda lost at this point.
So how does one do this ?? Is there some complete set of
"how-to-implement-your-own-classloader" ??
Can some of the Java-guru's help out ? I've already wasted TWO days on
this.. and this was that part of Java that I was looking forward to ..
- Chanda Dharap
------------------
email: chanda@prpa.philips.com
-
Note to Sun employees: this is an EXTERNAL mailing list!
Info: send 'help' to java-interest-request@java.sun.com