Java Serialization

Started by
3 comments, last by markr 18 years, 6 months ago
Does anyone know if Java can serialize objects with cross references (e.g. ClassA references ClassB and vice versa)? I'm trying to serialize an object of ClassA across the network and it fails to serialize one of it's references (i.e. ClassB). However when I try serialize an object of ClassB if fails to serialize its reference of ClassA. If anyone can offer any insight on this that would great.
Advertisement
Yes it can.

But both ClassA and ClassB have to be serializable. Otherwise it just ain't gonna work.

Oh yes, and the application on the other end needs to have ClassA and ClassB both in scope (i.e. visible to the loader), otherwise it'll be unserialising a nonexistent class, which doesn't work.

If you're using some kind of jar file optimiser or obfuscator, be sure that it doesn't optimise ClassB out.

What exception is it throwing?

Mark
both times it throws a ClassNotFoundException where the class not found is the object each is trying to reference (in this case each other). I looked over my code many times and I'm pretty sure each is implementing the Serializable interface (as well any innerclasses that are in the files). Plus I'm pretty sure that the applications on both end have the two classes. I'm not sure if I'm using a jar file optimiser or obfuscator. The thing is that I am using a pre-built framework to do my project so its possible that it is using this form of mechanism and I am just not aware of it. How can I found out if ClassB is being optimized out?
if you are using all default stuff like the standard netbeans setup, you aren't using an obfuscator, i guess. does the ClassNotFoundException not mention which class is causing problems?
Classes are never normally optimised out in Java.

Ensure that you are using identical versions of the application at both ends - ensure that the class files are the same.

Ideally, distribute client and server in the same .jar and just run with different parameters. Make sure they're the same version.

Mark

This topic is closed to new replies.

Advertisement