Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

SerialVersionUID

 
vineet kumar chaudhry
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I am sending a object through socket using serialization.The issue is

When I send it over a socket(socket.getOutputStream) and read on the server side ,it gives me java.IO.InvalidClassException.

However if I write in a file and then deserialize it in the same class, in which serialization was done,it works fine

Does this happen that sending object using socket causes errors

Even if I use to deserilaize the file which I created ,it is giving me java.IO.InvalidClassException.

I am using the same jar on the either side of socket.The difference is tthat at client side I am running thru eclipse so using jar in build path and ion server side I have kept the extracted folder from jar and placed in the current directory
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the class files on both sides are exactly the same you shouldn't get this error. It only occurs because the class files have different serialVersionUID values. This can easily occur if you add an extra field or method. You can prevent this error in two ways:
1) make sure that both sides really do have the exact same class files.
2) define an explicit private static final long serialVersionUID. Once you've defined all the fields, you can let Eclipse generate one for you. After that you can add, remove and change methods all you want, as long as the fields stay the same.

There are also tricks to be able to handle fields that are added or removed, but you really shouldn't use instances of different versions of the class.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic