gpt4 book ai didi

Java序列化: 'ClassNotFoundException' when deserializing an Object

转载 作者:行者123 更新时间:2023-12-01 23:38:14 27 4
gpt4 key购买 nike

错误:

java.lang.ClassNotFoundException: testprocedure.tp$3 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) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Unknown Source) at java.io.ObjectInputStream.resolveClass(Unknown Source) at java.io.ObjectInputStream.readNonProxyDesc(Unknown Source) at java.io.ObjectInputStream.readClassDesc(Unknown Source) at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source) at java.io.ObjectInputStream.readObject0(Unknown Source) at java.io.ObjectInputStream.defaultReadFields(Unknown Source) at java.io.ObjectInputStream.readSerialData(Unknown Source) at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source) at java.io.ObjectInputStream.readObject0(Unknown Source) at java.io.ObjectInputStream.readObject(Unknown Source) at core.ProcedureSetup.load(ProcedureSetup.java:57) at core.Engine.main(Engine.java:25)

我实例化该对象并从类“tp”中调用“ProcedureSetup”的“save”方法。

ProcedureSetup ps=new ProcedureSetup(new Procedure(){ public void doStuff(){ System.out.println("Stuff is being done"); }});
ps.save();

但是,我从不同的程序集合中加载,这些程序具有 -ALL- 所需的代码,但“tp”

ProcedureSetup ps=new ProcedureSetup();
ps.load();

类内对象的保存和加载:

public void load(){
String path=Operator.persistentGetFile();//gets the file path
ObjectInputStream ois=null;
FileInputStream fin=null;
ProcedureSetup temp=null;
try {
fin = new FileInputStream(path);
ois = new ObjectInputStream(fin);
temp=(ProcedureSetup) ois.readObject();
ois.close();
fin.close();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}

if(ois!=null){
try {
ois.close();
} catch (IOException e) {}
}
if(fin!=null){
try {
fin.close();
} catch (IOException e) {}
}
if(temp!=null){
a=temp.a;
}else{
load();//If a load is failed, restart process.
}
}

public void save(){
String path=Operator.persistentGetDirectory();//get directory to save to
String input = JOptionPane.showInputDialog(this, "Enter the File name:");
ObjectOutputStream oos=null;
FileOutputStream fon=null;
try {
fon = new FileOutputStream(path+input+".obj");
oos = new ObjectOutputStream(fon);
try {
oos.writeObject(this);
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
oos.close();
fon.close();
} catch (IOException e) {
e.printStackTrace();
}
if(oos!=null){
try {
oos.close();
} catch (IOException e) {}
}
if(fon!=null){
try {
fon.close();
} catch (IOException e) {}
}

}

我的问题是:

为什么会发生这些错误?

为什么(如果有必要)我的类路径中需要有“tp”?

如果实际上有一种方法可以将对象保存在当前状态,而无需在类路径中使用“tp”,我将如何做到这一点? (链接会很可爱)

最佳答案

当您读入序列化对象时,Java 通过使用序列化流中的信息来“重构”它,以构建该对象的实时副本。除非它具有对象类的 .class 文件,否则它无法执行此操作;它需要一个空白副本来“填写”流中的信息。

最好的选择通常是确保类位于类路径上。如果您有某些特殊原因导致此方法不起作用,那么 Java 序列化不适合您; JSON 可能是一个合适的选择。

关于Java序列化: 'ClassNotFoundException' when deserializing an Object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18331987/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com