Reputation: 1838
So this is my question.
I have a client and a server java program. Each one establishes and manages a ObjectInputStream/ObjectOutputStream with the other.
I want to be able to send objects (Given all objects sent will extend a Packet class I made). The issue is that even though the client and server may have the exact same code for the packet class, I am getting a error:
java.lang.ClassNotFoundException: shared.Packet0Connect
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at java.io.ObjectInputStream.resolveClass(Unknown Source)
at java.io.ObjectInputStream.readNonProxyDesc(Unknown Source)
at java.io.ObjectInputStream.readClassDesc(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at com.mooman219.gmail.server.ReceivePacketThread.run(ReceivePacketThread.java:19)
It appears that java thinks that the Packet class on the server is a different class on the client.
Is there a way I can fix this (Aside from having the same package names on both the client and server)?
Upvotes: 0
Views: 121
Reputation: 16605
The data transport object classes must be the same class -- they must have the same fully qualified class name (i.e. the same package name and the same classname), not just identical contents.
The byte stream emitted by the serialisation process includes the fully qualified class name of the object (as well as the FQCN of the superclasses). It then also includes the serialised versions of the non-transient, non-static fields (unless you've customised the serialisation process using one of the hook methods).
Here's an example of the byte stream. Note that in the example, the class is in the default (empty) package which makes the illustration of what we're discussing a bit less clear.
Upvotes: 2