Java.io.invalidClassException on serialization / deserialization

I have an object that I read and write to and from fileinputstreams/objectinputstreams and objectinputstreams/objectoutputstreams . I keep getting errors that java tried one serialversionUID but found another.

In my class, I implemented serializable and had a field like static final long serialVersionUID = 1L; which I thought was enough.

I am new to Java serialization. What am I missing here?

EDIT If that matters, I'm actually trying to write and read **ArrayList<MyObject>**

Here's the full trace:

 java.io.InvalidClassException: com.luxurymode.pojos.Reminder; Incompatible class (SUID): com.luxurymode.pojos.Reminder: static final long serialVersionUID =4209360273818925922L; but expected com.luxurymode.pojos.Reminder: static final long serialVersionUID =1L; W/System.err( 4504): at java.io.ObjectInputStream.verifyAndInit(ObjectInputStream.java:2723) W/System.err( 4504): at java.io.ObjectInputStream.readNewClassDesc(ObjectInputStream.java:1848) W/System.err( 4504): at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:826) W/System.err( 4504): at java.io.ObjectInputStream.readNewObject(ObjectInputStream.java:2066) W/System.err( 4504): at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:929) W/System.err( 4504): at java.io.ObjectInputStream.readObject(ObjectInputStream.java:2285) W/System.err( 4504): at java.io.ObjectInputStream.readObject(ObjectInputStream.java:2240) W/System.err( 4504): at java.util.ArrayList.readObject(ArrayList.java:662) W/System.err( 4504): at java.lang.reflect.Method.invokeNative(Native Method) W/System.err( 4504): at java.lang.reflect.Method.invoke(Method.java:521) W/System.err( 4504): at java.io.ObjectInputStream.readObjectForClass(ObjectInputStream.java:1537) W/System.err( 4504): at java.io.ObjectInputStream.readHierarchy(ObjectInputStream.java:1460) W/System.err( 4504): at java.io.ObjectInputStream.readNewObject(ObjectInputStream.java:2139) W/System.err( 4504): at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:929) W/System.err( 4504): at java.io.ObjectInputStream.readObject(ObjectInputStream.java:2285) W/System.err( 4504): at java.io.ObjectInputStream.readObject(ObjectInputStream.java:2240) W/System.err( 4504): at com.luxurymode.singletons.RemindersSingleton.<init>(RemindersSingleton.java:54) W/System.err( 4504): at com.luxurymode.singletons.RemindersSingleton.getInstance(RemindersSingleton.java:66) W/System.err( 4504): at com.luxurymode.views.AddReminderView.saveAlarm(AddReminderView.java:290) W/System.err( 4504): at com.luxurymode.tab_2.RemindersActivity.onClick(RemindersActivity.java:94) W/System.err( 4504): at android.view.View.performClick(View.java:2554) W/System.err( 4504): at android.view.View$PerformClick.run(View.java:8962) W/System.err( 4504): at android.os.Handler.handleCallback(Handler.java:587) W/System.err( 4504): at android.os.Handler.dispatchMessage(Handler.java:92) W/System.err( 4504): at android.os.Looper.loop(Looper.java:123) W/System.err( 4504): at android.app.ActivityThread.main(ActivityThread.java:4627) W/System.err( 4504): at java.lang.reflect.Method.invokeNative(Native Method) W/System.err( 4504): at java.lang.reflect.Method.invoke(Method.java:521) W/System.err( 4504): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860) W/System.err( 4504): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) W/System.err( 4504): at dalvik.system.NativeStart.main(Native Method) D/AndroidRuntime( 4504): Shutting down VM 
+6
source share
3 answers

Are you reading from a file? In this case, it does not matter if serialVersionUID is now added, it differs from the one stored in the file and throws an exception.

A quick solution could be to set serialVersionUID to 4209360273818925922L, which seems to be serialVersionUID, which was automatically generated by java when you saved this object in this file at that time :)

+7
source

As stated in the documentation, this can happen for three reasons:

  • The serial version of the class does not match the description of the class descriptor read from the stream
  • The class contains unknown data types.
  • The class has no no-arg constructor available.

So, first of all, make sure that both implementations have the same serialVersionUID . If this is true, you must be sure that the class does not use any type of undefined (or unknown) for the JVM to which you are trying to deserialize. Finally, you need to provide the standard constructor ClassName() , which performs empty initialization.

These can be problems and, of course, one of them, so I don’t think you should expect something strange. From my personal experience, I can also add that using different versions of the JVM for serialization and deserialization can create this problem, so be sure to verify this.

+6
source

Do you see the first line?
You have differenct serialVersionUID, they are incompatible with each other, you must use the same serialVersionUID!

java.io.InvalidClassException:

com.luxurymode.pojos.Reminder; Incompatible class (SUID): com.luxurymode.pojos.Reminder: static final long serialVersionUID = 4209360273818925922L ; but expected com.luxurymode.pojos.Reminder: static final long serialVersionUID = 1L ;

+3
source

Source: https://habr.com/ru/post/894154/


All Articles