Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Why java.io.InvalidClassException with Externalizable? (long post)

1 view
Skip to first unread message

Michel Langeveld

unread,
May 3, 2001, 12:16:31 AM5/3/01
to
Cut and pat the following 3 classfiles:

* MyTest2.java
* MichelWrite.java
* MichelRead.java

run it with:
* java MichelWrite file
* java MichelRead file

You will see an exception. Why is this?

C:\TMP\java\streams>java MichelWrite temp
WriteObject

C:\TMP\java\streams>java MichelRead temp
Exception in thread "main" java.io.InvalidClassException: MyTest2; <init>
at java.io.ObjectInputStream.inputObject(Compiled Code)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:369)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:232)
at MichelRead.main(MichelRead.java:15)
================= CUT HERE MyTest2.java =============
import java.io.*;
import java.util.*;

public class MyTest2 implements Externalizable
{
private transient double X, Y;
private double R;

public MyTest2(double x, double y, double radius)
{
this.X = x;
this.Y = y;
this.R = radius;
}

public boolean isWithin (double x, double y)
{
return (this.X - x) * (this.X - x) +
(this.Y - y) * (this.Y - y) <= this.R * this.R;
}

public void print()
{
System.out.println("x = " + X + ", Y = " + Y + ", R = " + R);
}

public void readExternal(ObjectInput objectIn) throws IOException
{
System.out.println("ReadObject");
X = objectIn.readDouble();
Y = objectIn.readDouble();
R = objectIn.readDouble();
}


public void writeExternal(ObjectOutput objectOut) throws IOException
{
System.out.println("WriteObject");
objectOut.writeDouble(X);
objectOut.writeDouble(Y);
objectOut.writeDouble(R);
}
}
=================================================

=================== CUT HERE MichelWrite.java =================

import java.io.*;

public class MichelWrite
{
public static void main(String argv[]) throws IOException
{
if (argv.length != 1)
{
throw (new IllegalArgumentException("Syntax: MichelWrite
<file>"));
}
ObjectOutputStream objectOut = new ObjectOutputStream( new
FileOutputStream(argv[0]));
try
{
MyTest2 m = new MyTest2(2, 3, 4);
objectOut.writeObject (m);
}
finally
{
objectOut.close();
}
}
}

=================================

============ CUT HERE MichelRead.java ================

import java.io.*;

public class MichelRead
{
public static void main(String argv[]) throws IOException,
ClassNotFoundException
{
if (argv.length != 1)
{
throw (new IllegalArgumentException("Syntax: MichelRead
<file>"));
}
ObjectInputStream objectIn = new ObjectInputStream( new
FileInputStream(argv[0]));
try
{
MyTest2 m;
m = (MyTest2)objectIn.readObject();
m.print();
}
finally
{
objectIn.close();
}
}
}

=======================

Gordon Beaton

unread,
May 3, 2001, 3:02:44 AM5/3/01
to
On Thu, 3 May 2001 06:16:31 +0200, Michel Langeveld wrote:
> Cut and pat the following 3 classfiles:
>
> * MyTest2.java
> * MichelWrite.java
> * MichelRead.java
>
> run it with:
> * java MichelWrite file
> * java MichelRead file
>
> You will see an exception. Why is this?

MyTest2 needs a default (no-argument) constructor. This constructor is
needed in order to create the (empty) object before calling
readExternal() on it.

Why do you also externalize X and Y, when you've declared them
transient?

/gordon

--
[ do not send me private copies of your followups ]
g o r d o n . b e a t o n @ e r i c s s o n . c o m
ericsson research
stockholm, sweden

Michel Langeveld

unread,
May 3, 2001, 1:35:18 PM5/3/01
to
Darn that did the trick!!

I made that variable transient just for test purpose and because it would
then
be a better reason for this class to do the serialization itselfs.

Thank you very much!!

"Gordon Beaton" <not.fo...@see.signature> wrote in message
news:9cqvqk$l4g$1...@news.du.uab.ericsson.se...

0 new messages