File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Loading POJO object without loading library first Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Loading POJO object without loading library first" Watch "Loading POJO object without loading library first" New topic

Loading POJO object without loading library first

Alex Finn

Joined: Jan 12, 2006
Posts: 5
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
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

Joined: Mar 22, 2005
Posts: 42965
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

Joined: Jan 12, 2006
Posts: 5
Doesn't help me in anyway.
But sorry i was not aware
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

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:

[Jess in Action][AskingGoodQuestions]
Alex Finn

Joined: Jan 12, 2006
Posts: 5
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

Joined: Jan 12, 2006
Posts: 5
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

Joined: Jan 12, 2006
Posts: 5
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
I agree. Here's the link:
subject: Loading POJO object without loading library first
It's not a secret anymore!