简体   繁体   中英

Why am I getting ClassNotFoundException with ObjectInputStream.readObject()?

The following client code accepts a "Guest" object from the server:

protected static void sendLoginRequest(String email, String password) {
    try(
        Socket socket = new Socket("127.0.0.1", 44444);
        BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        BufferedWriter out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
    ){
        out.write("LGNR");
        out.flush();
        out.newLine();
        out.flush();

        out.write(email);
        out.flush();
        out.newLine();
        out.flush();            

        out.write(password);
        out.flush();
        out.newLine();
        out.flush();

        in.ready();
        switch(in.readLine()) {
        case "SU":
            Guest g = null;
            try(ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());){
                g = (Guest) ois.readObject();
            }catch(ClassNotFoundException | IOException e) {
                System.out.println(e.getMessage());
                e.printStackTrace();
            }
            GuestProfileMenu.generateMenu(g);
            break;
        case "WP":
            System.out.println("Invalid Password.");
            break;
        case "IE":
            System.out.println("Email not found.");
            break;
        }

    }catch(IOException e) {
        System.out.println("No response from Server.");
        System.out.println(e.getMessage());
    }
}

And on the server side it looks like this:

private void userLogin(Socket socket, BufferedReader in, BufferedWriter out) {
    System.out.println(socket.getInetAddress().getHostName() + ":" + socket.getLocalPort() + " - has sent a login request.\r\n");

    FileManager.openGuestFile();
    ArrayList<Guest> gList = FileManager.getGuestList();

    try{
        in.ready();
        String email = in.readLine();
        System.out.println(socket.getInetAddress().getHostName() + ":" + socket.getLocalPort() + " - Email received as: " + email);

        in.ready();
        String password = in.readLine();
        System.out.println(socket.getInetAddress().getHostName() + ":" + socket.getLocalPort() + " - Password received as: " + password);

        boolean exists = false;

        for(int i = 0; i < gList.size(); i++) {
            if (email.equals(gList.get(i).getEmail())) {
                exists = true;
                if (password.equals(gList.get(i).getPassword())) {                      
                    out.write("SU");
                    out.flush();
                    out.newLine();
                    out.flush();
                    try(ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());){            
                        oos.writeObject(gList.get(i));
                        oos.flush();
                    }catch(IOException e) {
                        System.out.println(e.getMessage());
                    }
                }else{
                    out.write("WP");
                    out.flush();
                    out.newLine();
                    out.flush();
                }
            }
        }
        if(!exists) {
            out.write("IE");
            out.flush();
            out.newLine();
            out.flush();
        }
        socket.close();
    }catch(IOException e) {
        System.out.println(e.getMessage());
    }

}

From the client side it returns a ClassNotFoundException at the line "g = (Guest) ois.readObject();":

java.lang.ClassNotFoundException: server.Guest
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)

The fact that the error message says "server.Guest", ie (class package).(object), can I assume that an instance of the server's Guest class isn't the same thing as an instance of the client's Guest class? So how do I properly send a receive objects through a socket?

Thanks

The fact that the error message says "server.Guest", ie (class package).(object), can I assume that an instance of the server's Guest class isn't the same thing as an instance of the client's Guest class?

That two separate classes are involved is what the exception is telling you, yes.

So how do I properly send a receive objects through a socket?

Limit yourself to classes that client and server both know. There is a variety of ways to accomplish this, but they pretty much all involve deploying the same or equivalent .class files on both sides of the connection. "Equivlent" here means approximately "compiled from the same source with compilers of the same version".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM