Java.io.InvalidClassException:

InvalidClassException: local class incompatible: stream classdesc serialVersionUID = -196410440475012755, local class serialVersionUID = -6675950253085108747

I am structuring with an InvalidClassException in the following script. Here my EAR is installed on 4 Websphere application servers, and this is sharing. Sometimes I get an InvalidClassException from a POJO class that implements the Serializable interface. Please tell me this. I have no information about this.

+8
source share
5 answers

When you implement the java.io.Serializable interface to create a serializable class, the compiler searches for a static final field called "serialVersionUID" of type long. If the class has not specified this field explicitly, then the compiler will create one such field and assign it a value that leaves the implementation-dependent serialVersionUID calculation. This calculation depends on various aspects of the class, and it follows the specifications for object serialization given by Sun. But, in all compiler implementations the same value is not guaranteed.

, - . Serialization , serialVersionUID ( ) (, , , - ) . serialVersionUID, , , InvalidClassException.

NOTE-- long "serialVersionUID" , Serializable, , . , , , InvalidClassException , .

'private' , , , . , - , 'private'.

+33

, , ( ) .

serialVersionUID , Serializable, ( transient) . , ( , ).

- : , , ( ).

, , (.. ).

+9

- EJB TimerTask - - , WebSphere Application Server:

bin bin 2 / , EJB. , serialVersionUID. , . :

findEJBTimers.bat cancelEJBTimers.bat

, . , , .

+2

, :

PoJos

. , "JSON" , , . , pojo , , /how/ ( , JVM). , JSON , XML - . . XML .

    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.3.1</version>
        <scope>test</scope>
    </dependency>

  @Test
    public void testJSON() throws Exception {
        Foo expected = new Foo(1,"Christian",1000000.00d);
        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        String testJson = gson.toJson(expected);

        System.out.println(testJson);

        Foo result = gson.fromJson(testJson, Foo.class);
        assertEquals(expected,result);

    }

    public static class Foo {

        private String name;
        private Integer age;
        private Double paycheck;

        public Foo(Integer age, String name, Double paycheck) {
            this.age = age;
            this.name = name;
            this.paycheck = paycheck;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;

            Foo foo = (Foo) o;

            if (age != null ? !age.equals(foo.age) : foo.age != null) return false;
            if (name != null ? !name.equals(foo.name) : foo.name != null) return false;
            if (paycheck != null ? !paycheck.equals(foo.paycheck) : foo.paycheck != null) return false;

            return true;
        }

        @Override
        public int hashCode() {
            int result = name != null ? name.hashCode() : 0;
            result = 31 * result + (age != null ? age.hashCode() : 0);
            result = 31 * result + (paycheck != null ? paycheck.hashCode() : 0);
            return result;
        }
    }

{
  "name": "Christian",
  "age": 1,
  "paycheck": 1000000.0
}
0
source

When an object is serialized, serialVersionUID is serialized along with other content.

Later, when this is deserialized, the serialVersionUID from the deserialized object is retrieved and compared to the serialVersionUID of the loaded class.

If the numbers do not match, an InvalidClassException is thrown.

0
source

All Articles