L
Luca Rosellini
Hi everybody, I have this problem with classloaders:
Suppose to have two nodes A and B, node A creates a new class C1 (in the package "Package") which implements an interface (call it myInterface) known to both node A and B.
A retrieves the bytecode for C1, stores it in a byte[] and send it in a datagram to node B.
In order to properly load the class on B's JVM I actually inherit from ClassLoader and override loadClass method in this way:
class myClassLoader extends ClassLoader{
...
...
public Class loadClass(String className, boolean res){
// check if the clas has not yet been loaded
// blah blah
byte[] classData = buffer_containing_bytecode;
Class result = defineClass(className, classData, 0, classData.length);
...
...
return result;
}
}
when I need to load the class in my program I do the following (in a try-catch clause):
myClassLoader cl = new myClassLoader();
Class myClass = cl.loadClass("P.C1",true);
loadClass seems to return successfully, if I use reflection on myClass object to get informations on methods or fields of the class I get correct results.
This leads me to think that class is loaded properly.
However if I try to create an Instance from myClass using myClass.newInstance() I get an InstantiationException.
Any of you knows what I am doing wrong?
P.S.: There's another solution that works, but I don't like it, it looks much more like a workaround than a solution and a write on disk is needed:
when the bytecode arrives on node B I dump it in a file called "C1.class" in a folder called"Package" using a File a object and calling deleteOnExit() on that File object.
when tring to create a new instance from object myClass everything as expected.
Suppose to have two nodes A and B, node A creates a new class C1 (in the package "Package") which implements an interface (call it myInterface) known to both node A and B.
A retrieves the bytecode for C1, stores it in a byte[] and send it in a datagram to node B.
In order to properly load the class on B's JVM I actually inherit from ClassLoader and override loadClass method in this way:
class myClassLoader extends ClassLoader{
...
...
public Class loadClass(String className, boolean res){
// check if the clas has not yet been loaded
// blah blah
byte[] classData = buffer_containing_bytecode;
Class result = defineClass(className, classData, 0, classData.length);
...
...
return result;
}
}
when I need to load the class in my program I do the following (in a try-catch clause):
myClassLoader cl = new myClassLoader();
Class myClass = cl.loadClass("P.C1",true);
loadClass seems to return successfully, if I use reflection on myClass object to get informations on methods or fields of the class I get correct results.
This leads me to think that class is loaded properly.
However if I try to create an Instance from myClass using myClass.newInstance() I get an InstantiationException.
Any of you knows what I am doing wrong?
P.S.: There's another solution that works, but I don't like it, it looks much more like a workaround than a solution and a write on disk is needed:
when the bytecode arrives on node B I dump it in a file called "C1.class" in a folder called"Package" using a File a object and calling deleteOnExit() on that File object.
when tring to create a new instance from object myClass everything as expected.