来自ObjectInputStream.readObject的超级奇怪的ClassNotFoundException

时间:2018-04-19 06:18:44

标签: java serialization

尝试从ObjectInputStream加载序列化对象并获得此神秘错误:

java.lang.ClassNotFoundException: mypackage.MyClass in
 thread mythread
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:348)
    at java.io.ObjectInputStream.resolveClass(ObjectInputStream.java:628)
    at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1620)
    at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1521)
    at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1781)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1353)
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:373)

该类与调用readObject的方法位于同一个类路径中。一切都有serialVersionUID。

当我这样做时

Class.forName("mypackage.MyClass") 

就在readObject之前它运行得很好。

WTF可能导致这种情况?

0 个答案:

没有答案