1

我尝试从服务器反序列化类,但在客户端我得到:

java.lang.ClassNotFoundException: com.server.core.StateFilesStruct

在我的客户端中,具有此包名称的 StateFilesStruct 类在哪里:

com.client.core.StateFilesStruct

这是我的课:

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;
}

我发送这个的类在服务器中:

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>();
    }
}

我在客户端中保留的类与服务器类相同,但包名称不同:

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>();
    }
}

我试图解决客户端中的类的方法是:使用 ZeroMQ 从 tcpip 套接字接收

 byte[] byteFileStruct = m_pNetworkManager.getSocket().recv(0); 
 StateFilesStruct stateFilesStruct = (StateFilesStruct)deserialize(byteFileStruct);

得到这个异常,因为你可以看到它从服务器类中寻找:

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)
4

1 回答 1

3

在java中,包名是类名的一部分。就 JVM 而言,这些是 2 个完全不同的类,因此您需要在类路径中反序列化相同的类。

于 2013-01-29T19:56:05.777 回答