• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Dynamically load a class and its object

 
Ranch Hand
Posts: 178
2
Netbeans IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This question was asked in a different forum too. But I didn't get an answer..

I have a class file and a saved object of that class. My java application doesn't have that class in its libraries. When I tried to load the object using ObjectInputStream, it gives an ClassNotFoundException.

Then I use the following code.



But this code also gives me the same exception when reading the object. Hope for a help to do my task..
MyClass doesn't have a package name.
FileIOManager.readObject() is a method I created to read a object and it works fine for other object which have classes inside my libraries.

 
Marshal
Posts: 80068
410
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What about Class.forName()?
You would have to do some sort of casting if you deserialise that object.

Not certain, but I think you cannot do that without having access to the .class file in a .jar or otherwise.
 
Ramesh Pramuditha Rathnayake
Ranch Hand
Posts: 178
2
Netbeans IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried that too. The result is same. ClassNotFoundException in 3rd line.

As I see, the loaded class by classLoader is not accessible for the ObjectInputStream. If there is an another way to load a class to ObjectInputStream manually, I think my problem will be solved..
 
Campbell Ritchie
Marshal
Posts: 80068
410
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't know about loading things with an object input stream, not without a .class to create the object from.

Anybody else?
 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Yeah, you can't deserialize an instance, if you don't have the class file in the classpath and/or have an incompatible class file in the classpath. As for using your own class loader, I agree that that seems to be a voodoo subject -- so good luck with figuring it out.

Henry
 
Ramesh Pramuditha Rathnayake
Ranch Hand
Posts: 178
2
Netbeans IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I found the answer..

There is a method in ObjectInputStream class called, resolveClass(ObjectStreamClass desc). It is called once every time the Stream meets a new type of object. The default implementation of this method is,

where loader is the default ClassLoader of the JVM.

I just had to override this method with my own ClassLoader. I edited my readObject() method in FileIOManager class as follows.


While the people in other forum go way even without looking at my problem, here you talked to me and inspired me with your ideas even though you didn't have an answer. Thank you for that..


 
Campbell Ritchie
Marshal
Posts: 80068
410
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome and well done sorting it out and posting the answer.
 
Henry Wong
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for coming back and reporting the answer. This helps give the topic some closure, and could help someone else in the future. Have a cow.

Henry
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic