hi!
i wrote a short code of client, server. when i send the object from the client to server, it throws exception in the server: java.lang.ClassNotFoundException: client.Person (Person is the object).
client:
package client; import java.net.*; import java.io.*; public class SimpleClient { public static void main(String args[]){ try{ Socket s = new Socket("localhost",2002); OutputStream os = s.getOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(os); InputStream is = s.getInputStream(); ObjectInputStream ois = new ObjectInputStream(is); oos.writeObject(new String("another object from the client")); oos.writeObject(new Person ()); oos.writeObject("hello!!!"); System.out.println((String)ois.readObject()); Person p = (Person)ois.readObject(); System.out.println(p.age + ", " + p.name + ", " + p.id); System.out.println((String)ois.readObject()); oos.close(); os.close(); s.close(); } catch(Exception e) {System.out.println(e);} } }
server:
package server; import java.net.*; import java.io.*; public class SimpleServer { public static void main(String args[]) { int port = 2002; try { ServerSocket ss = new ServerSocket(port); Socket s = ss.accept(); InputStream is = s.getInputStream(); ObjectInputStream ois = new ObjectInputStream(is); OutputStream os = s.getOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(os); System.out.println((String)ois.readObject()); Person p = (Person)ois.readObject(); System.out.println(p.age + ", " + p.name + ", " + p.id); System.out.println((String)ois.readObject()); oos.writeObject("hello!!!"); oos.writeObject(p); oos.writeObject(new String("another object from the server 2 client")); is.close(); s.close(); ss.close(); } catch(Exception e) {System.out.println(e);} } }
class Person - exists in the both packages
import java.io.Serializable; class Person implements Serializable { String id = "012345678"; int age = 30; String name = "Inigo Montoya"; }
anyone knows why do i get this exception?
thank you,
Moshiko