Question: "obj= (DATA)ois.readObject(); "which is in class User. After compiling,I got a message indicates "unreported exception java.lang.ClassNotFoundException; must be caught or declared to be thrown " what does that means?
I did implements Serializable in DATA class. Please tell me what is wrong or what the error message means.
the following is my code import java.io.*; import java.net.*; /*using readObject() to read the DATA object*/ public class serSock{
private int port=8765; private ServerSocket myServer; private User user; serSock(){ try{ myServer= new ServerSocket(port); // setting up Server System.out.println("Server is set up"); }catch(IOException e){ e.printStackTrace(); System.exit(1); } }
public void execute(){ try{ user = new User(myServer.accept(),this); // Server listening user.start(); System.out.println("Server is listening"); }catch(IOException e){ e.printStackTrace(); System.exit(1); }
}
public static void main(String args[]) { serSock ser1= new serSock(); ser1.execute();
}
}
class User extends Thread { Socket conn; ObjectInputStream ois; serSock control; DATA obj=null;
System.out.println("The user's IP is " + conn.getInetAddress()); try{ obj= (DATA)ois.readObject();
}catch(IOException e){e.printStackTrace(); //System.out.print(obj.getOrigin() + " From server's user class"); System.out.println("This is from user run function");}
according to the documentation of readObject this method throws ClassNotFoundException so in your run method either have a try..catch block enclosing ois.readObject() or specify ClassNotFoundException using throws clause.
hey, thanks for ur response. I did put a try-catch block enclosing ois.readObject()statement, however, the error message still occurs. Is anything else u could think of that might be the reason cause it happenes?
publicvoid run(){
System.out.println("The user's IP is " + conn.getInetAddress());
try{
obj= (DATA)ois.readObject();
}catch(IOException e){
e.printStackTrace();
//System.out.print(obj.getOrigin() + " From server's user class");
System.out.println("This is from user run function");
} catch (ClassNotFoundException ex){
System.err.println("YOUR PROBLEM WAS HERE...");
}
try{
conn.close();
}catch (IOException e){}
}