Help - java.io.InvalidClassException

M

Mario Rosario

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...
}
---------------------------------------------------------------------
 
L

Lothar Kimmeringer

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: (e-mail address removed)
PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

Always remember: The answer is forty-two, there can only be wrong
questions!
 
M

Mario Rosario

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 said:
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
 
R

rkm

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 said:
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?

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
 
M

Mario Rosario

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

rkm said:
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
 
S

SteveE

Mario said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top