查找哪个jar 中的哪个类具有给定的serialVersionUID
当我收到 java.io.InvalidClassException 时,它会为我提供它想要的serialVersionUID 以及它获得的serialVersionUID。 有没有一种简单的方法来判断我的几十个罐子中的哪一个使用了错误的serialVersionUID?
更新:我应该提到,我们的目的是同时更新所有内容,但我正在尝试调试构建和部署过程中的问题。
When I get a java.io.InvalidClassException, it gives me the serialVersionUID that it wants, and the serialVersionUID that it got. Is there an easy way to tell which of my dozens of jars using the wrong serialVersionUID?
Update: I should mention that our intention is to update everything at the same time, but I'm trying to debug a problem in our build and deploy process.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
对 jar 中的每个类使用 sun jdk 中的 Serialver 工具。
use the serialver tool from the sun jdk for each class in the jar.
处理这种麻烦的最好办法就是同时更新服务器端和客户端的jar。 这将保证双方的类版本相同,并且在序列化/反序列化时不会遇到问题。 每次遇到此问题时跟踪串行 UID 并不能解决任何问题,只会浪费大量时间和资源。 最好花一些时间实施正确的部署/打包策略。
如果您确实没有任何其他选择,您可以编写一个从每个 jar 加载类的工具(使用 URLClassLoader),然后使用 java.io.ObjectStreamClass.getSerialVersionUID() 获取您需要的信息。
The best way to deal with this kind of trouble is to update jars on the server and client side at the same time. This will guarantee the same version of your classes on both sides and you'll not have trouble when serializing / deserializing. Tracking serial UIDs each time you have this problem is not going to solve anything, you're only going to waste considerable time and resources. Its much better to spend some time and implement a proper deployment / packaging strategy.
If you really don't have any other choice, you can write a tool that loads a class from each jar (using a URLClassLoader) and then use
java.io.ObjectStreamClass.getSerialVersionUID()
to obtain the information you need.请记住,各种 JVM/容器/类加载器组合对于应该从 bootclasspath 与 application/webapp 类路径加载哪些类有不同的看法。
由于 Serialver 总是首先从 bootclasspath 加载,所以这很复杂,因此您可能需要使用 -J-Xbootclasspath 来模拟不同的行为:
另一种方法是使用 javap,例如:
Bear in mind that various JVM/container/classloader combinations have different opinions on which classes should be loaded from the bootclasspath versus the application/webapp classpath.
This is complicated by the fact that serialver always loads from the bootclasspath first, so you may need to use -J-Xbootclasspath as below, to simulate different behaviour:
Another approach is to use javap, for example:
根据 SO 中的一些答案,我想出了一个实用程序来执行此操作。 这是我的班级。
I came up with a utility to perform this, based on some answers in SO. Here's my class.
笨拙但有效
我会使用逆向工程工具
1)解压罐子
2) 在类文件树上运行 jad
3) 在这个新的源树上运行 grep 或任何其他查找工具来查找串行版本 uid。
clumsy but works
i would use a reverse engineering tool
1) unzip the jars
2) run, say, jad on the class file tree
3) run grep or any other find tool on this new tree of source to look for the serial version uid.
我在 Linux 上使用此命令来检查当前目录中的所有 Jars:
请记住,serialVersionUID 可能未在您的类中显式声明。 在这种情况下,serialVersionUID 将在运行时计算,并且无法使用上面的 find 命令进行搜索。
I was using this command on Linux to examine all Jars in current directory:
Please bear in mind that serialVersionUID might be not declared in your class explicitly. In that case serialVersionUID will be calculated at runtime and cannot be searched for using find command above.