Hi guys heres an interesting problem I'm having I want to be able to load a pojo object of a class that I didn't load on runtime
So lets say I download a file, and class from somewhere and I want to continue to run whats in that object, lets say I have X.instance and X.class
I have code to create a new instance of that class no problem but if I try loading the object I get a ClassNotFoundException
But this does work
Any thoughts I've tried creating a new instance of this object and having code in there to load an object and it has the same result.
I have code using reflections to be able to use this object after so thats not a problem.
Any help would be awesome thanks Thanks Alex F. (Yes I know this is an odd use but I need to be able to load any object aka using POJO using a class I didn't load)
Joined: Mar 22, 2005
Hello "Alex F"-
On your way in you may have missed that JavaRanch has a policy on display names, and that yours does not comply with it. Please adjust it accordingly, which you do right here. Thanks for your prompt attention to this matter.
The only work around I've found is to add it dynamically through The System ClassLoader, but this means I will fill that up very quickly and the only way to delete the files already loaded (not not used) is to create the same jar with empty classes and reimport, which will make the system run much slower
Joined: Jan 12, 2006
Heres all the code that I'm trying at the moment
And heres the code to Dynamic Class (which should be compiled and moved out of the way of any classpath
I get this error when trying all 3 File statements (2 are commented out)
This is really a head scratcher. Thanks in advanced for any help you try to give me. Alex Finn
Joined: Jan 12, 2006
Ok heres the deal, I figured it out, you guys were right EXCEPT the fact that there is a bug in ObjectInputStream, that automatically uses the SystemClassLoader. Therefore this means that I can't set the context.
To fix this you have to subclass the ObjectInputStream and overwrite the method resolveClass
The code should look like this
Thanks for all your help I think you guys got me to find the right answer Alex