i try to deserialize class from the server , but in the client im getting :
java.lang.ClassNotFoundException: com.server.core.StateFilesStruct
where in my client the StateFilesStruct class with this package name:
com.client.core.StateFilesStruct
here is my class’s :
public Object deserialize(byte[] bytes) {
ByteArrayInputStream b = new ByteArrayInputStream(bytes);
ObjectInputStream o = null;
try {
o = new ObjectInputStream(b);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
return o.readObject();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return o;
}
the class im sending this is in the server :
package com.server.core;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Hashtable;
public class StateFilesStruct implements Serializable {
private static final long serialVersionUID = 988633010151085730L;
private Hashtable<String,String> Repository;
public Hashtable<String, String> getRepository() {
return Repository;
}
public void setRepository(Hashtable<String, String> repository) {
Repository = repository;
}
public StateFilesStruct()
{
Repository = new Hashtable<String,String>();
}
}
the class i keep in the client the same as the server one but with different packge name:
package com.client.core;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Hashtable;
public class StateFilesStruct implements Serializable {
private static final long serialVersionUID = 988633010151085730L;
private Hashtable<String,String> Repository;
public Hashtable<String, String> getRepository() {
return Repository;
}
public void setRepository(Hashtable<String, String> repository) {
Repository = repository;
}
public StateFilesStruct()
{
Repository = new Hashtable<String,String>();
}
}
and the way im trying to resolve the class in the client is :
receiving from tcpip socket using ZeroMQ
byte[] byteFileStruct = m_pNetworkManager.getSocket().recv(0);
StateFilesStruct stateFilesStruct = (StateFilesStruct)deserialize(byteFileStruct);
getting this exception as you can see its looking fro the server class :
java.lang.ClassNotFoundException: com.server.core.StateFilesStruct
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
In java, the package name is part of the class name. As far as the JVM is concerned these are 2 completely different classes, so you need to have the same class you’re deserializing in your classpath.