简体   繁体   中英

Java issue setting serialVersionUID

I am currently trying to write a multiplayer space-invaders-like game where the client runs on a computer and the server on an android device. For that reason I need to transmit the position of all the objects via network, I'm using serialization. At first I had this class for the game data, which was not thread-safe, but could be serialized and deserialized just fine:

public class gamestate implements Serializable{

  public ship firstplayer;
  public ship secondplayer;
  public ship[] enemies;

}

As I said, that was not thread-safe, so I tried adding an empty method which would force Java to re-fetch the object before proceeding:

synchronized void v(){}

This is where the problems began. Even though I have exactly the same code on PC and phone, the generated serialVersionUIDs did not match, so I tried setting them myself.

  static final long serialVersionUID = 1000L;

This had the following result:

ru.black.ksserver.gamestate; local class incompatible: stream classdesc serialVersionUID = 1000, local class serialVersionUID = -4828470651897483247

It seems like setting it on android worked perfectly fine while setting it on PC did not. Can anyone help me with this?

If you are going to use an explicit serialVersionUID in a class, you need to:

  • use the same value for serialVersionUID in all copies of the class
  • make sure that you deal with any differences what is included in the serialization.

If you don't use an explicit serialVersionUID in a class, then the versions of the classes at both ends must have the same default serial version. The Object Serialization spec explains how the default serial version is computed.


It seems like setting it on android worked perfectly fine while setting it on PC did not.

Actually, it looks to me like you are trying to read an instance that was serialized with serialVersionUID 1000 in a class that doesn't have an explicit serialVersionUID variable. That won't work.

Did you actually change the class to add the explicit serialVersionUID variable on the PC side? It looks like you didn't .....

I (kinda) solved it and its a bug. If I run my program using the latest JDk it completely ignores this field, it only does what it's supposed to when using a older JDK. Did they change something in the latest update? Or maybe my installation is just broken...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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