• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

what are the reasons for: invalidclassexception?

 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey,
i have a sequential access file with objects given to me by my instructor. he has asked that we "duplicate" the class he created. the only fields in the class are a couple of String(s),an int, and 3 accessor methods. when i try to read the from the stream i get the exception above. now he insists that i don't have to set the serialVersionUID in my version of his class, which i have found will work, but that i spend time trying to mimic the constructor and method signatures he used originally and that this will 'compute' to the same serialVersionUID. my question(s): is he correct? and what are the possible causes/reasons for an InvalidClassException? any salient help will... thanks

nikita
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The API documentation for InvalidClassException says:


Thrown when the Serialization runtime detects one of the following problems with a Class.

  • The serial version of the class does not match that of the class descriptor read from the stream
  • The class contains unknown datatypes
  • The class does not have an accessible no-arg constructor
  •  
    Ranch Hand
    Posts: 368
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    In general, the serialization mechanizm is enough robust, and will successfully load your class even if there are some changes in the code and/or fields, but the implementation protects you, providing some hash value that catches the changes done in the code of your class. The serialVersionUID is the way to declare the value by hand, if you can guarantee that you version of the class is enough compatible with previous versions.
    So I guess your instructor is correct if your changes to the class do not result in an inability to de-serialize.
    [ September 13, 2008: Message edited by: Satya Maheshwari ]
     
    f. nikita thomas
    Ranch Hand
    Posts: 87
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    okay.....
    but is it true that if i duplicate the class as he originally defined it, it will hash out to the same serialVersionUID as the class objects in the file. here's what i have so far:



    and a test run:



    i didn't use a try/catch block in order to show the exception. if anyone wants "yankees.yanks", pm me and i'll gladly give it to ya. if i take out the constant, it hashes to a different value. this should have been a excercise in rote, but like most things i have learned in java
    there is always something more... so if anyone can explain the mechanism behind this, (like how java creates the serial version), i'd really appreciate it. take care.
    [ September 13, 2008: Message edited by: f. nikita thomas ]
     
    f. nikita thomas
    Ranch Hand
    Posts: 87
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    found the following here:
    http://java.sun.com/j2se/1.5.0/docs/guide/serialization/spec/class.html#4100



    so...





    i deleted the .class file and recompiled:



    so my instructor was correct. then i decided to change the constructor signature:



    and then:



    i changed the identifiers as well which had no effect. i changed the access modifiers and:





    this is the same id that i found during reading the stream and receiving an exception. so i learned something.

    btw, i didn't see the need to move this post to the intermediate section. i'm an undergraduate at a community college. i feel that the subject material would have been better left there. take care.
    [ September 13, 2008: Message edited by: f. nikita thomas ]
     
    Satya Maheshwari
    Ranch Hand
    Posts: 368
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    so i learned something



    Great. Same here.
     
    The only taste of success some people get is to take a bite out of you. Or this tiny ad:
    Smokeless wood heat with a rocket mass heater
    https://woodheat.net
    reply
      Bookmark Topic Watch Topic
    • New Topic