• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Loading POJO object without loading library first

 
Alex Finn
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Enjoy your time here.
 
Alex Finn
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Doesn't help me in anyway.
But sorry i was not aware
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You just need to set the "context class loader" when you do the deserialization to the ClassLoader that can find the class, something like this:

 
Alex Finn
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried using 1.4.2 and 1.5(5.0)


I tried it with using it in a jar file and trying to use the file itself .class and the directory to which the package was in.

java.lang.ClassNotFoundException: newObj.testObj

Heres what I have which isn't any different





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

Any thoughts
 
Alex Finn
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Alex Finn
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic