Non Serializable Objects

I am working on a java project and I have to serialize a class (created by me) which has many references to other classes that should be serialized too.

I add the marker "implements serializable" to all the necessary classes, but when I run I get a non serializable exception.

Maybe one ore more objects are non serializable, but how do I identify them?

I've read that If I have to serialize non serializable objects, I need to write my own writeObject(ObjectOutputStream out)throws IOException and readObject(ObjectInputStream in) , but I don't know how to implement and us them.

note : I can't use transient beacuse I need everything to be serialized!

Thanks a lot. Bye!

[712 byte] By [madcillaa] at [2007-11-26 14:49:34]
# 1
the object that is not serializable should be mentioned in the exception's stacktrace; check that
TheStijna at 2007-7-8 8:37:35 > top of Java-index,Core,Core APIs...
# 2

Now I'll post my code, If anyone knows how to serialize (and then deseserialize) the class "AgentMessage" , and its subclasses "StaticPart" and "DynElement" from the main class , I'll be grateful.. It's about 2 days I'm working on it and I continue getting "nonserialializable exception" ...

thank you guys

package agentsLibrary;

//some imports

public class AgentMessage implements Serializable{

private static final long serialVersionUID = 1;

private StaticPart sp;

private DynElement de;

public AgentMessage(String agent,byte[] code,String mainclass,Certificate signerId,PrivateKey priv,String configuration,Serializable dclear,byte[] dsecret,PathEl[] dpath,byte[] c){ //costruttore

sp=new StaticPart(agent,code,mainclass,signerId,priv,configuration);

de=new DynElement(dclear,dsecret,c,dpath);

}

public StaticPart getSp(){

return sp;

}

public DynElement getDe(){

return de;

}

private void writeObject(java.io.ObjectOutputStream out) throws IOException{

System.out.println("class implements writeObject( )");

//depends on the method to store out all the important state

//out.defaultWriteObject();//perform the default serialization(va sempre fatto il default)

out.writeObject(sp);

out.writeObject(de);

}

private void readObject(java.io.ObjectInputStream in)throws ClassNotFoundException, IOException {

System.out.println("class implements readObject( )");

//in.defaultReadObject();

sp=(StaticPart)in.readObject();

de=(DynElement)in.readObject();

}

}

*******************************************************

package agentsLibrary;

public class StaticPart implements Serializable{

private static final long serialVersionUID = 1;

private String agent="";

private byte[] code=null;

private String mainclass="";

private Certificate signerid=null;

private PrivateKey priv=null;

private Calendar timestamp=null;

private Signature sig=null;

private byte[] buffertotale=null;

private byte[] firma=null;

private String configuration="";

public StaticPart(String agent, byte[] code, String mainclass, Certificate signerid, PrivateKey priv,String configuration){

this.configuration=configuration;

this.code=code;

this.mainclass=mainclass;

this.agent=agent;

this.priv=priv;

this.signerid=signerid;

timestamp=Calendar.getInstance();

Date time=new Date();

time=timestamp.getTime();//Gets this Calendar's current time.

try {

byte[] nomeagent=agent.getBytes("8859_1");//converto stringa

byte[] mainclas=mainclass.getBytes("8859_1");//converto stringa

byte[] signer=signerid.getEncoded();

byte[] priva=priv.getEncoded();

byte[] dat=null;

dat=time.toString().getBytes("8859_1");

buffertotale=new byte[nomeagent.length+mainclas.length+signer.length+priva.length+dat.length+code.length];

System.arraycopy(nomeagent, 0, buffertotale, 0, nomeagent.length);

System.arraycopy(code, 0, buffertotale, nomeagent.length, code.length);

System.arraycopy(mainclas, 0, buffertotale, code.length, mainclas.length);

System.arraycopy(signer, 0, buffertotale, mainclas.length, signer.length);

System.arraycopy(priva, 0, buffertotale, signer.length, priva.length);

System.arraycopy(dat, 0, buffertotale, priva.length, dat.length);

} catch (UnsupportedEncodingException e) {

e.printStackTrace();

System.exit(1);

} catch (CertificateEncodingException e) {

e.printStackTrace();

System.exit(1);

}

try {

sig = Signature.getInstance(priv.getAlgorithm());

sig.initSign(priv);

sig.update(buffertotale, 0, buffertotale.length);

firma=sig.sign();

} catch (SignatureException e) {

e.printStackTrace();

System.exit(1);

} catch (InvalidKeyException e) {

e.printStackTrace();

System.exit(1);

} catch (NoSuchAlgorithmException e) {

e.printStackTrace();

System.exit(1);

}

}

public boolean verify() {

try{

PublicKey pub=signerid.getPublicKey();

Signature sig = Signature.getInstance(pub.getAlgorithm());

sig.initVerify(pub);

sig.update(buffertotale, 0, buffertotale.length);

return sig.verify(firma);

} catch (SignatureException e) {

e.printStackTrace();

System.exit(1);

} catch (InvalidKeyException e) {

e.printStackTrace();

System.exit(1);

} catch (NoSuchAlgorithmException e) {

e.printStackTrace();

System.exit(1);

}

return false;

}

public String getAgentName() {

return agent;

}

public byte[] getCode() {

return code;

}

public String getClassName() {

return mainclass;

}

public PrivateKey getPrivate() {

return priv;

}

public Certificate getId() {

return signerid;

}

public byte[] getSignature(){

return firma;

}

public String getConfiguration(){

return configuration;

}

}

***********************************************

package agentsLibrary;

import java.io.Serializable;

public class DynElement implements java.io.Serializable{

private static final long serialVersionUID = 1;

private Serializable dclear=null;

private byte[] dsecret=null;

private PathEl[] dpath=null;

private byte[]c=null;

public DynElement(Serializable dclear,byte[] dsecret,byte[]c,PathEl[] dpath){

this.dclear=dclear;

this.dsecret=dsecret;

this.c=c;

this.dpath=dpath;

}

public byte[] getC() {

return c;

}

public Serializable getDclear() {

return dclear;

}

public PathEl[] getDpath() {

return dpath;

}

public byte[] getDsecret() {

return dsecret;

}

}

*********************************************

//finally the following is the main class that should serialize

....

AgentMessage msg=new AgentMessage(name,code,mainclass,signerId,privata,configuration,dclear,dsecret,dpath,c);

try {

System.out.println("Sending Agent Message to Server "+ip+":"+port);

Socket s = new Socket(ip,port);

ObjectOutputStream out=new ObjectOutputStream(s.getOutputStream());

out.writeObject(msg);

out.flush();

s.close();

} catch (Exception e) {

return false;

}

return true;

}

madcillaa at 2007-7-8 8:37:35 > top of Java-index,Core,Core APIs...
# 3

>the object that is not serializable should be mentioned in the exception's stacktrace; check that

I agree. You might try in the catch block of AgentMessage main, where you return false a line such System.out.println("There was an error writing the object to the stream: " + e.toString() );

Or if you prefer, you could replace e.toString() with e.printStackTrace(). If this is where your error is happening, it should then print something like NotSerializableException: class name.

kazea at 2007-7-8 8:37:35 > top of Java-index,Core,Core APIs...
# 4
Please check if my reply at http://forum.java.sun.com/thread.jspa?threadID=5123280 helps.
hemalpandyaa at 2007-7-8 8:37:35 > top of Java-index,Core,Core APIs...