I am having trouble de-serializing Objects. I keep getting "class not found" exceptions when I compile certain programs that I've written. Others, using the same imports & in the same folder compile just fine & I can't see what the problem is. I have a pretty good knowledge of Java but this one has me stumped. I've spent the last 2 days on the Internet, found lots of good stuff, but I'm still baffled as to why I'm getting these errors. I understand that the class files have to be available to de-serialize, but when it won't compile, no class files obviously are generated!! It is not difficult code & appears to be the standard way to write/read Objects. Somewhere, my Object "signatures" must be getting lost/changed whatever. A couple of examples follow. All of the files are in a folder c:\java\scratch. The client compiles, the server throws a "class not found" exception & won't compile (hopefully no brackets/semicolons were lost during copy/paste). If anyone has any suggestions of sees what's wrong, I'd appreciate some feedback! Thanks...
public class ClientTest implements Serializable { public ClientTest(){}
public static void main(String[] args) { try { Hashtable settings = new Hashtable(); settings.put("buffers", new Integer(20)); settings.put("layers", new Integer(2)); settings.put("title", "navigator");
InetAddress ia = InetAddress.getByName("localhost"); Socket socket = new Socket(ia, 6789); DataOutputStream dos = new DataOutputStream(socket.getOutputStream()); ObjectOutputStream oos = new ObjectOutputStream(dos); //socket.getOutputStream()
oos.writeObject(settings); // new Date() oos.flush(); oos.close(); socket.close(); } catch(IOException e) { System.err.println("Threw an IOException!" + e); } } }
After days of surfing & not figuring out what the problem was I stumbled upon it today. It seems that if you use catch(IOException + e) in a try/catch block when writing/reading objects instead of (Exception + e) (or maybe some other error reporting routine), it will throw a "class not found" exception. That's about the only thing I never considered changing in my code!!! I'm sure the API contains the reason why, but for now, I'm just glad I figured it out, quite by accident. Maybe this will help other people in the future!!! -Jeff