So I have a program (which is a simple chat client), and I’m trying to send, from the server, an updated client. To do this, I’m making use of the serializable keyword. The existing client can load and execute code from this kind of object transmission, so that’s all good. My issue is that the program features a few inner classes. Now, the way I’m doing this is to create an array consisting of all the class files necessary for the update, then iterating through it and sending them off one at a time. My problem is that the order in which they are sent has an effect on execution. If I send the inner classes first, the code works, and the client updates, but throws “ClassDefNotFound” exceptions any time one of the inner classes is used. Alternatively, if I send the inner classes last, the code does not work at all, and the client does not update.
private static final Class[] clientUpdates =
{EntryPanel.EnterSendListener.class, EntryPanel.ButtonSendListener.class,
EntryPanel.class, MessagePanel.class, GUIClient.ClientThread.class, GUIClient.class,
UpdateClientMessage.class};
Is my array, where anything with a path in is an inner class. The last element is one that runs a method starting the new client when it is sent.
for(Class c : clientUpdates)
{
String name = c.getName();
InputStream is = c.getClassLoader().getResourceAsStream(
name.replaceAll("\\.", "/") + ".class");
ByteArrayOutputStream os = new ByteArrayOutputStream();
byte[] buff = new byte[1024];
int i = is.read(buff);
while(i != -1)
{
os.write(buff, 0, i);
i = is.read(buff);
}
byte[] bs = os.toByteArray();
NewMessageType m = new NewMessageType(name, bs);
queue.put(m);
}
queue.put(new UpdateClientMessage("localhost", port));
Is the code I’m using to send the class definitions. NewMessageType is a class designed to contain raw byte data of a class and transmit it across, to be read on the other side. Putting things on the Queue simply transmits them across.
I’ll be the first to admit that I’m not wonderful at networking stuff, and serialisability is not my strong suit. However, I could really do with some help here. It would be greatly appreciated.
I fixed this, by the way. The issue was not to do with inner classes, but circular dependencies (i.e. two classes both requiring the definition of the other). In order to remove these, I added some abstract classes that one class in the dependency referred to, and the other inherited. In this way, as long as I sent the abstract ones first, there would be no issue.