读取序列化对象java会导致classnotfound异常
以下是我当前的代码:读取序列化对象java会导致classnotfound异常,java,serialization,classnotfound,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(ob
//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异常。发生了什么事?所有要反序列化的类都必须存在于包含导入代码的项目的类路径上。ClassNotFoundException
哪个类的ObjectInputStream。它在.readObject()上给出它;。不,发布堆栈跟踪。等等,我必须让User
类与导入代码位于同一文件夹中?不,你必须让类在类路径中可用,正如他已经告诉你的那样。