Java SerialIzation:反序列化对象时出现“ ClassNotFoundException” [英] Java SerialIzation: 'ClassNotFoundException' when deserializing an Object
问题描述
错误:
java.lang.ClassNotFoundException:testprocedure.tp $ 3
位于java.net.URLClassLoader $ 1。 run(未知源)
在java.net.URLClassLoader $ 1.run(未知源)
在java.security.AccessController.doPrivileged(本机方法)
在java.net.URLClassLoader.findClass (未知来源)java.lang.ClassLoader.loadClass上的
(未知来源)sun.misc.Launcher $ AppClassLoader.loadClass(java.lang.ClassLoader.loadClass上的
) (未知来源)java.lang.Class.forName0处的
(本机方法)java.lang.Class.forName处的
(未知源)java.io.ObjectInputStream.resolveClass处的
(未知源)java.io.ObjectInputStream.readNonProxyDesc上的
(未知源)java.io.ObjectInputStream.readClassDesc上的
(未知源)java.io.ObjectInputStream.readOrdinaryObject上的
(未知源) java.io.ObjectInputStream.readOb上的
ject0(未知源)
在java.io.ObjectInputStream.defaultReadFields(未知源)
在java.io.ObjectInputStream.readSerialData(未知源)在Java.io.ObjectInputStream.readOrdinaryObject(未知源)java.io.ObjectInputStream.readObject0处的
(未知源)java.io.ObjectInputStream.readObject处
(未知源)core.ProcedureSetup.load(ProcedureSetup.java: 57)core.Engine.main(Engine.java:25)上的
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)
我实例化该对象并调用
I instantiate the object and call the "ProcedureSetup"'s "save" method from Class "tp".
ProcedureSetup ps=new ProcedureSetup(new Procedure(){ public void doStuff(){ System.out.println("Stuff is being done"); }});
ps.save();
但是我从具有-ALL-所需代码但 tp
however I load from a different collection of programs that has -ALL- required code but "tp"
ProcedureSetup ps=new ProcedureSetup();
ps.load();
在类内保存和加载对象:
Object saving and loading within class:
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) {}
}
}
我的问题是:
为什么会发生这些错误?
Why are these errors happening?
为什么(如有必要)我的类路径中是否需要包含 tp?
Why (if necessary) do I need to have "tp" in my classpath?
如果实际上有一种方法可以将对象保存为当前状态而无需使用 tp在类路径中,我将如何去做? (链接会很可爱)
If there is in fact a way to save the object in its current state with out the necessity of "tp" in the classpath how would I go about doing that? (Links would be lovely)
推荐答案
new Procedure(){ public void doStuff(){ System.out.println("Stuff is being done"); }}
上面是您的 tp $的匿名内部类c $ c>类。因此,要反序列化,此匿名内部类及其封闭类
tp
必须出现在类路径中:字节流包含类名和字段
The above is an anonymous inner class of your tp
class. So, to be deserialized, this anonymous inner class, and its enclosing class tp
, must be present in the classpath: the stream of bytes contains the name of the class and the fields of the object, but it doesn't contain the byte-code of the class itself.
您应该将其设置为顶级类,或者至少将其设置为 static 内部类。
You should make it a top-level class, or at least a static inner class.
您还应该遵守Java命名约定:类为CamelCased。
You should also respect the Java naming conventions: classes are CamelCased.
这篇关于Java SerialIzation:反序列化对象时出现“ ClassNotFoundException”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!