Help - java.io.InvalidClassException

Discussion in 'Java' started by Mario Rosario, Sep 19, 2003.

  1. Hi,

    I am getting an error and I don't understand why:

    I have an object Item which I serialize within a servlet and send it across
    to an applet but I get this error "java.io.InvalidClassException:
    com.webacre.servlet.Item; class invalid for deserialization". I tried
    changing all compile target to 1.1 but it didn't help. Does anyone know what
    I am doing wrong? What do I need to do to make this work?

    Thanks for your help.
    Mario

    ---------------Applet.java---(compile target Java 1.1)------------
    import com.webacre.servlet.Item;
    ... <stuff deleted> ...

    v = new Vector();
    ObjectInputStream in = new ObjectInputStream(con.getInputStream());
    while ((item = (Item)in.readObject()) != null) {
    v.addElement(item);
    }
    -------------------------------------------------------------------
    ---------------Servlet.java----(compile target Java 1.4)-----------
    import com.webacre.servlet.Item;
    ... <stuff deleted> ...

    ObjectOutputStream out = new ObjectOutputStream(res.getOutputStream());
    for (int i=0; i < list.size(); i++) {
    out.writeObject((Item)list.get(i));
    }
    -------------------------------------------------------------------

    ----------- Item.java -----(compile target Java 1.4)----------------
    package com.webacre.servlet;
    import java.io.Serializable;

    public class Item implements Serializable {

    private int id = -1;
    private double width = 0.0;
    private String catalog = "-";

    public int getItemId() { return this.id; }
    public void setItemId(int i) { this.id = i; }
    ...etc...
    }
    ---------------------------------------------------------------------
     
    Mario Rosario, Sep 19, 2003
    #1
    1. Advertising

  2. On Fri, 19 Sep 2003 14:53:09 -0700, Mario Rosario wrote:

    > I have an object Item which I serialize within a servlet and send it across
    > to an applet but I get this error "java.io.InvalidClassException:
    > com.webacre.servlet.Item; class invalid for deserialization".


    Taken from the Javadoc:

    http://java.sun.com/j2se/1.4.2/docs/api/

    public class InvalidClassException
    extends ObjectStreamException

    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

    Since:
    JDK1.1

    The first thing can happen if there was some kind of conversion
    of the characters in the stream (happens often, when the characters
    were converted to byte and back to char.

    The second one should explain itself.

    The third one as well.


    Regards, Lothar
    --
    Lothar Kimmeringer E-Mail:
    PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

    Always remember: The answer is forty-two, there can only be wrong
    questions!
     
    Lothar Kimmeringer, Sep 19, 2003
    #2
    1. Advertising

  3. Mario Rosario

    Roedy Green Guest

    Roedy Green, Sep 20, 2003
    #3
  4. I tried adding this "static final long serialVersionUID = 99999999999999L;"
    to Item as suggested but still getting the InvalidClassException error. I am
    stumped! Anyone know what is wrong?

    "Mario Rosario" <> wrote in message
    news:bkfthb$ett$...
    > Hi,
    >
    > I am getting an error and I don't understand why:
    >
    > I have an object Item which I serialize within a servlet and send it

    across
    > to an applet but I get this error "java.io.InvalidClassException:
    > com.webacre.servlet.Item; class invalid for deserialization". I tried
    > changing all compile target to 1.1 but it didn't help. Does anyone know

    what
    > I am doing wrong? What do I need to do to make this work?
    >
    > Thanks for your help.
    > Mario
    >
    > ---------------Applet.java---(compile target Java 1.1)------------
    > import com.webacre.servlet.Item;
    > ... <stuff deleted> ...
    >
    > v = new Vector();
    > ObjectInputStream in = new ObjectInputStream(con.getInputStream());
    > while ((item = (Item)in.readObject()) != null) {
    > v.addElement(item);
    > }
    > -------------------------------------------------------------------
    > ---------------Servlet.java----(compile target Java 1.4)-----------
    > import com.webacre.servlet.Item;
    > ... <stuff deleted> ...
    >
    > ObjectOutputStream out = new ObjectOutputStream(res.getOutputStream());
    > for (int i=0; i < list.size(); i++) {
    > out.writeObject((Item)list.get(i));
    > }
    > -------------------------------------------------------------------
    >
    > ----------- Item.java -----(compile target Java 1.4)----------------
    > package com.webacre.servlet;
    > import java.io.Serializable;
    >
    > public class Item implements Serializable {
    >

    static final long serialVersionUID = 99999999999999L

    > private int id = -1;
    > private double width = 0.0;
    > private String catalog = "-";
    >
    > public int getItemId() { return this.id; }
    > public void setItemId(int i) { this.id = i; }
    > ...etc...
    > }
    > ---------------------------------------------------------------------
    >
    >
    >
     
    Mario Rosario, Sep 24, 2003
    #4
  5. Mario Rosario

    rkm Guest

    Using 1.1 target is a good suggestion you should stick with,
    even if it doesn't immediately solve your problem. That
    removes a potential source of incompatibility at least.

    I don't see anything else about the code that seems
    problematic but you could try adding a public no-args
    constructor to Item, maybe the servlet side needs to invoke
    it as part of the reconstitution process.

    Rick

    Mario Rosario wrote:
    > I tried adding this "static final long serialVersionUID = 99999999999999L;"
    > to Item as suggested but still getting the InvalidClassException error. I am
    > stumped! Anyone know what is wrong?
    >
    > "Mario Rosario" <> wrote in message
    > news:bkfthb$ett$...
    >
    >>Hi,
    >>
    >>I am getting an error and I don't understand why:
    >>
    >>I have an object Item which I serialize within a servlet and send it

    >
    > across
    >
    >>to an applet but I get this error "java.io.InvalidClassException:
    >>com.webacre.servlet.Item; class invalid for deserialization". I tried
    >>changing all compile target to 1.1 but it didn't help. Does anyone know

    >
    > what
    >
    >>I am doing wrong? What do I need to do to make this work?
    >>
    >>Thanks for your help.
    >>Mario
    >>
    >>---------------Applet.java---(compile target Java 1.1)------------
    >>import com.webacre.servlet.Item;
    >> ... <stuff deleted> ...
    >>
    >> v = new Vector();
    >> ObjectInputStream in = new ObjectInputStream(con.getInputStream());
    >> while ((item = (Item)in.readObject()) != null) {
    >> v.addElement(item);
    >> }
    >>-------------------------------------------------------------------
    >>---------------Servlet.java----(compile target Java 1.4)-----------
    >>import com.webacre.servlet.Item;
    >> ... <stuff deleted> ...
    >>
    >> ObjectOutputStream out = new ObjectOutputStream(res.getOutputStream());
    >> for (int i=0; i < list.size(); i++) {
    >> out.writeObject((Item)list.get(i));
    >> }
    >>-------------------------------------------------------------------
    >>
    >>----------- Item.java -----(compile target Java 1.4)----------------
    >>package com.webacre.servlet;
    >>import java.io.Serializable;
    >>
    >>public class Item implements Serializable {
    >>

    >
    > static final long serialVersionUID = 99999999999999L
    >
    >
    >> private int id = -1;
    >> private double width = 0.0;
    >> private String catalog = "-";
    >>
    >> public int getItemId() { return this.id; }
    >> public void setItemId(int i) { this.id = i; }
    >> ...etc...
    >>}
    >>---------------------------------------------------------------------
    >>
    >>
    >>

    >
    >
    >
     
    rkm, Sep 24, 2003
    #5
  6. Thanks Rick, that didn't solve the problem either. I'm baffeled!
    You mean adding "Item() {}"

    "rkm" <> wrote in message
    news:...
    > Using 1.1 target is a good suggestion you should stick with,
    > even if it doesn't immediately solve your problem. That
    > removes a potential source of incompatibility at least.
    >
    > I don't see anything else about the code that seems
    > problematic but you could try adding a public no-args
    > constructor to Item, maybe the servlet side needs to invoke
    > it as part of the reconstitution process.
    >
    > Rick
    >
    > Mario Rosario wrote:
    > > I tried adding this "static final long serialVersionUID =

    99999999999999L;"
    > > to Item as suggested but still getting the InvalidClassException error.

    I am
    > > stumped! Anyone know what is wrong?
    > >
    > > "Mario Rosario" <> wrote in message
    > > news:bkfthb$ett$...
    > >
    > >>Hi,
    > >>
    > >>I am getting an error and I don't understand why:
    > >>
    > >>I have an object Item which I serialize within a servlet and send it

    > >
    > > across
    > >
    > >>to an applet but I get this error "java.io.InvalidClassException:
    > >>com.webacre.servlet.Item; class invalid for deserialization". I tried
    > >>changing all compile target to 1.1 but it didn't help. Does anyone know

    > >
    > > what
    > >
    > >>I am doing wrong? What do I need to do to make this work?
    > >>
    > >>Thanks for your help.
    > >>Mario
    > >>
    > >>---------------Applet.java---(compile target Java 1.1)------------
    > >>import com.webacre.servlet.Item;
    > >> ... <stuff deleted> ...
    > >>
    > >> v = new Vector();
    > >> ObjectInputStream in = new ObjectInputStream(con.getInputStream());
    > >> while ((item = (Item)in.readObject()) != null) {
    > >> v.addElement(item);
    > >> }
    > >>-------------------------------------------------------------------
    > >>---------------Servlet.java----(compile target Java 1.4)-----------
    > >>import com.webacre.servlet.Item;
    > >> ... <stuff deleted> ...
    > >>
    > >> ObjectOutputStream out = new

    ObjectOutputStream(res.getOutputStream());
    > >> for (int i=0; i < list.size(); i++) {
    > >> out.writeObject((Item)list.get(i));
    > >> }
    > >>-------------------------------------------------------------------
    > >>
    > >>----------- Item.java -----(compile target Java 1.4)----------------
    > >>package com.webacre.servlet;
    > >>import java.io.Serializable;
    > >>
    > >>public class Item implements Serializable {
    > >>

    > >
    > > static final long serialVersionUID = 99999999999999L
    > >
    > >
    > >> private int id = -1;
    > >> private double width = 0.0;
    > >> private String catalog = "-";
    > >>
    > >> public int getItemId() { return this.id; }
    > >> public void setItemId(int i) { this.id = i; }
    > >> ...etc...
    > >>}
    > >>---------------------------------------------------------------------
    > >>
    > >>
    > >>

    > >
    > >
    > >

    >
    >
     
    Mario Rosario, Sep 24, 2003
    #6
  7. Mario Rosario

    SteveE Guest

    Mario Rosario wrote:
    > Thanks Rick, that didn't solve the problem either. I'm baffeled!
    > You mean adding "Item() {}"


    Just a few thoughts:

    - try: public Item() {}
    - ensure you are using an identical package.Item class for both server
    and applet
    - if you are testing using a browser, clear it's cache
    - you might want to calc the proper serialVersionUID using the
    'serialver' tool, although this should be necessary if both are the same
    - try using out.useProtocolVersion(ObjectOutputStream.PROTOCOL_VERSION_1)

    That's all I can think of atm.

    SteveE
     
    SteveE, Sep 24, 2003
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dave Rudolf
    Replies:
    3
    Views:
    4,470
    Sudsy
    Dec 20, 2003
  2. Aidan
    Replies:
    6
    Views:
    1,074
    Aidan
    Jun 4, 2004
  3. Nomak
    Replies:
    11
    Views:
    1,889
    Arnaud Berger
    Apr 18, 2005
  4. Raga

    InvalidClassException!

    Raga, Sep 26, 2006, in forum: Java
    Replies:
    10
    Views:
    4,429
    shweta.naik
    Aug 22, 2007
  5. Replies:
    1
    Views:
    376
    Manish Pandit
    Jun 18, 2007
Loading...

Share This Page