我会尝试尽可能简洁明了地解释我的问题。我觉得我所尝试的可能本质上是错误的,但不知何故我相信它应该起作用。因此,请随意将此帖作为对序列化机制的澄清。实现类的接口引用的序列化在解序列化时失败
所以,我有一个由类实现的接口[它暗示它们存在于单独的文件中]。
// ----- SomeInterface.java
public interface SomeInterface extends Serializable
{
public void setMsg(String msg);
public void printMsg();
}
// ----- SomeImpl.java
public class SomeImpl implements SomeInterface
{
private String msg;
public void setMsg(String msg)
{
this.msg = msg;
}
public void printMsg()
{
System.out.println("Msg is: " +msg);
}
}
现在我序列[TT] SomeImpl [/ TT]类的一个实例:
...
public void SomeMethInSomeClass()
{
FileOutputStream fos = new FileOutputStream(new File("c:/test.ser"));
ObjectOutputStream oos = new ObjectOutputStream(fos);
SomeInterface si = new SomeImpl();
si.setMsg("blah blah");
oos.writeObject(test);
oos.close();
fos.close();
System.out.println("------ done -------");
}
现在,在一个单独的项目,我有上述含有界面SomeInterface
应用的JAR,但不是执行类SomeImpl
。在这种应用中,如果我尝试做到以下几点:
....
....
public void deserialiserMethod() throws Exception
{
FileInputStream fis = new FileInputStream(new File("c:/test.ser"));
ObjectInputStream ois = new ObjectInputStream(fis);
SomeInterface si = null;
System.out.println("------ reading ----------");
si = (SomeInterface) ois.readObject();
si.printMsg();
System.out.println("------- done ----------");
ois.close();
fis.close();
}
....
...我得到一个ClassNotFoundException
[为SomeImpl
]。现在我知道我的其他项目在其类路径中没有SomeImpl
类。但是不应该将其转换为父接口的引用来处理该依赖关系?
感谢您的意见!
'resolveClass'和'annotateClass'看起来很有希望。我会研究他们。谢谢汤姆! – anirvan 2010-07-19 19:13:01