刚序列化的类反序列化期间的ClassNotFoundException [英] ClassNotFoundException during Deserialization of a just-serializaed class

查看:113
本文介绍了刚序列化的类反序列化期间的ClassNotFoundException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我反序列化对象时遇到问题。当前项目具有插件样式的体系结构,因此我有一些jar,其中包含在运行时加载的类文件。我无法反序列化包含其中一个罐中找到的类的对象,因此我编写了一种称为中流的快速测试方法,该方法刚刚加载了插件,实例化了正确的插件(该对象实现了特定的接口因此我可以通过.isAssignableFrom(..)来识别它,对其进行序列化(这很好),然后立即尝试对其进行反序列化。

I was having an issue with deserializing an object. The current project has a plugin-style architecture, so I have jars that contain class files that are loaded at runtime. I was unable to deserialize an object that contained a class that was found in one of those jars, so I wrote a quick test method that gets called mid-stream that just loaded the plugins, instantiates the correct one (the object implements a particular interface so I can identify it via .isAssignableFrom(..) ), serializes it (which happens fine), and then immediately tries to deserialize it.

我仍然收到 ClassNotFoundException。

I still get a 'ClassNotFoundException'.

Stacktrace:

Stacktrace:

Jul 21, 2014 4:02:11 PM com.newspinrobotics.auth.MainFrame loadPlugins
SEVERE: null
java.lang.ClassNotFoundException: com.newspinrobotics.auth.plugin.tcpserver.TCPServer
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:270)
at java.io.ObjectInputStream.resolveClass(ObjectInputStream.java:625)
at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1612)
at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1517)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1771)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1350)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:370)
at com.newspinrobotics.auth.MainFrame.loadPlugins(MainFrame.java:79)
at com.newspinrobotics.auth.MainFrame.<init>(MainFrame.java:43)
at com.newspinrobotics.auth.MainFrame$6.run(MainFrame.java:539)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:733)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:703)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

现在,在您询问之前。 TCPServer类中没有不可序列化的字段。它具有字符串和基元。有一个字段不属于这些类别,但被标记为瞬态。甚至有一个无参数的构造函数(虽然不需要不需要,对吗?)。是的,它实现了Serializable(序列化很好)。

Now, before you ask. The TCPServer class has no non-serializable fields in it. It has Strings and primitives in it. There is one field that does not fall into those categories but it is marked transient. There is even a no-arg constructor (though that is not needed, correct?). And yes it implements Serializable (the serialization goes fine).

我很困惑类加载器可能没有它,因为它在反序列化对象之前仅实例化了几行。

I am confused how the classloader could possibly NOT have it since it instantiates the object only a few lines before it deserializes it.

我确实使用定制的ClassLoader(扩展了URLClassLoader)在运行时加载jar文件。

I do use a custom-made ClassLoader (extends URLClassLoader) to load the jar files at runtime.

我确实提供了一个公共静态最终长long serialVersionUID = XXXXXXXL;字段。

I do provide a public static final long serialVersionUID = XXXXXXXL; field.

编辑:

我所指的类加载器扩展了URLClassLoader 。它位于另一个人编写的实用程序类中,在进一步检查时,实际上它甚至根本不是修改(出于某种原因,他觉得想要对其进行扩展,而实际上并未对其做任何实质性的改动)。该实用程序所做的全部工作就是拆开jar文件,并使用URLClassLoader通过addURL(..)将jar文件添加到URLClassLoader中,并通过loadClass(..)加载类。因此,该实用程序似乎没有任何恶意。但是我不是ClassLoader忍者,所以如果需要的话,我当然可以提供更多信息。

The Class loader I am referring to extends URLClassLoader. It's located inside a utility class written by another person that upon further inspection actually is not really even a modification (for some reason he felt like extending it and not really doing anything of substance to it). All the utility does is pick apart jar files, and use the URLClassLoader to add the jar files into the URLClassLoader via addURL(..) and also load the classes via loadClass(..). So it does not seem like there's anything nefarious in this utility. I am no ClassLoader ninja however, so I can certainly give further info on it if needed. It's really just a few utility functions for loading Jar files and picking out class files and loading them.

帮我StackOverflow,这是我唯一的希望(也许)。

/ p>

Help me StackOverflow, you're my only hope (maybe).

推荐答案

您是否将 ObjectInputStream 子类化以覆盖 resolveClass ()使用自定义的 Classloader ? (有关不是这样做的直接答案的示例,请参见 ObjectInputStream自定义类加载器反序列化问题:未调用resolveClass()。)

Did you subclass ObjectInputStream to override resolveClass() to use your custom Classloader? (For an example of someone else doing this, though not a direct answer to your question, see ObjectInputStream custom classloader deserialization issue: resolveClass() not called.)

这篇关于刚序列化的类反序列化期间的ClassNotFoundException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆