简体   繁体   English

具有不同Java版本的不同serialVersionUID

[英]Different serialVersionUID with different Java Versions

i have a weird Problem with the Java serialization/de-serialization. 我对Java序列化/反序列化有一个奇怪的问题。

Besides all other problems with serialization and the proper usage of serialVersionUIDs such a behaviour is completely new to me: 除了序列化的所有其他问题以及serialVersionUID的正确用法之外,这种行为对我来说是全新的:

On one machine my software is running on (openSuse 13.1) a update switched the Java version to Java 8. Now, i get a java.io.InvalidClassException if i try to reopen a project file of the software (which is a serialized Java Object) because one of the included classes has the wrong serialVersionUID. 我的软件在一台机器上运行(openSuse 13.1),更新将Java版本切换为Java8。现在,如果我尝试重新打开软件的项目文件(这是序列化的Java对象),我将收到java.io.InvalidClassException。 ),因为其中一个包含的类具有错误的serialVersionUID。 The weird thing is, that these class is a class from an external library and has not changed several month (but has no hard coded serialVersionUID field). 奇怪的是,这些类是来自外部库的类,并且几个月没有更改(但没有硬编码的serialVersionUID字段)。

If i switch back to Java 7 on that machine, the error do not occur. 如果我在该计算机上切换回Java 7,则不会发生该错误。 I can reproduce that error on other machines with openSuse 13.1 + Java 8 but not with machines with Windows or other Linux distributions (like Ubuntu) with Java 8 (or Java 7). 我可以在其他带有openSuse 13.1 + Java 8的计算机上重现该错误,但不能在具有Windows或其他具有Java 8(或Java 7)的Linux发行版(例如Ubuntu)的计算机上重现该错误。

On all machines the official sun/oracle JRE package is installed. 在所有计算机上,都安装了官方的sun / oracle JRE软件包。

So, has anybody faced that problem at any time? 那么,有人在任何时候都面临过这个问题吗? I do not think that it is a bug in Java 8, because it only occurs on an openSuse 13.1 machine. 我不认为这是Java 8中的错误,因为它仅发生在openSuse 13.1计算机上。 But how could the guys from openSuse mess up their system such, that java would hash the class in a different way as before (as Java has not changed that process in a new version)? 但是,来自openSuse的人们如何将他们的系统弄乱,以至于Java将以与以前不同的方式对类进行哈希处理(因为Java在新版本中并未更改该过程)?

EDIT: To be clear, the class who causes the problem is from an external library. 编辑:明确地说,导致问题的类是来自外部库。 It has not changed in month. 它没有改变一个月。 And the solution can not be to define a serialVersionUID, because that error has some other sources and would still be there. 解决方案不能是定义serialVersionUID,因为该错误还有其他来源,并且仍然存在。 And i think, it would not stop with this class, it would affect all classes from my used libraries with no defined serialVersionUID 而且我认为,它不会以此类结束,它将影响我使用的库中所有未定义serialVersionUID的类

EDIT2: Here is the exact error: EDIT2:这是确切的错误:

java.io.InvalidClassException: edu.uci.ics.jung.graph.util.Pair; local class incompatible: stream classdesc serialVersionUID = 7664847375082415686, local class serialVersionUID = -638192081897624765 

On all other machines i can test it, the class has the hashed serialVersionUID 7664847375082415686, only openSuse 13.1 + Java 8 get a result of -638192081897624765 在我可以测试的所有其他机器上,该类具有哈希的serialVersionUID 7664847375082415686,只有openSuse 13.1 + Java 8得到了-638192081897624765的结果

has anybody faced that problem at any time? 有没有人随时面对这个问题?

You have done well to have not found this issue. 您做得很好,没发现此问题。 All the IDEs have a warning for Serializable classes with a serialVersionUID AFAIK. 所有IDE都对带有serialVersionUID AFAIK的可序列化类发出警告。 It's a common issue, or rather it is common for people to hardcode the serialVersionUID so they don't see it. 这是一个常见问题,或者更常见的是,人们对serialVersionUID进行硬编码,以便他们看不到它。 Note: eclipse and Oracle JDKs produce different UID for the same version of Java. 注意:eclipse和Oracle JDK为相同版本的Java产生不同的UID。

because it only occurs on an openSuse 13.1 machine. 因为它仅发生在openSuse 13.1机器上。

This would be very surprising. 这将非常令人惊讶。 The same version of Java should produce the same serialVersionUID on every OS. 相同的Java版本应在每个OS上产生相同的serialVersionUID。

Note: you can set the serialVersionUID to any thing you need. 注意:您可以将serialVersionUID设置为所需的任何内容。 eg say you need to read an object with a given serialVersionUID, you can set it to the expect UID and it will attempt to read any version you want. 例如,说您需要使用给定的serialVersionUID读取对象,可以将其设置为期望的UID,它将尝试读取所需的任何版本。

We found out a while back that the source of our problem was because we had an active service running in the background that was compiled with an older version of Java. 一段时间以前,我们发现问题的根源是因为我们有一个在后台运行的活动服务,该服务是使用较旧版本的Java编译的。 This service was set to start automatically. 该服务被设置为自动启动。 Therefore, rebooting the computer didn't work for obvious reasons. 因此,由于明显的原因,重新启动计算机无法正常工作。 This was the result of a defect on our software uninstaller. 这是我们的软件卸载程序存在缺陷的结果。 We often install builds on our machines to test fielded versions of our software. 我们经常在计算机上安装内部版本以测试软件的现场版本。 In this particular case, in one machine there was an active service that was conflicting with our software; 在这种情况下,在一台机器上有一个活动服务与我们的软件冲突; even though we were running it from Eclipse. 即使我们从Eclipse运行它。

I would suggest to anyone experiencing similar issues to verify that an old service is not creating the conflict. 我建议任何遇到类似问题的人,以验证旧服务是否没有造成冲突。 Make sure you uninstall or disable the suspect service and try again. 确保您卸载或禁用可疑服务,然后重试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM