读取序列化对象java会给出classnotfound异常

时间:2013-12-26 22:02:30

标签: java serialization classnotfound

这是我目前的代码:

//export method
public static void exportObj (Object obj, String fname) {
    try {
        // Serialize data object to a file
        ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(fname));
        out.writeObject(obj);
        out.close();
    } catch (IOException e) {}
}
//import method
public static Object importObj (String fname) {
    try {
        ObjectInputStream in = new ObjectInputStream(new FileInputStream(fname));
        return in.readObject();
    } catch (IOException e) {}
    return new Object();
}

导出函数工作正常,我认为,它将我的User对象转换为文件并保存它,但是当我尝试导入它时,它给了我一个ClassNotFound异常。发生了什么事?

1 个答案:

答案 0 :(得分:0)

要反序列化的所有类必须存在于包含导入代码的项目的CLASSPATH中。