Question

I am writing an app that makes use of Object Output and Input Streams. However I have a problem, because I am not able to send my object properly. I write it to stream, and server gives me a class not found exception, even though both client and server have exactly the same copy of this class (the only difference is the package name) with the same serial id. Here is my class:

import java.io.Serializable;

public class Message implements Serializable {

/**
 * 
 */
private static final long serialVersionUID = 1L;
private String username = null;
private String hashedPassword = null;
private Integer code = null;
private String from = null;
private String to = null;
private Object data = null;

public Message() {

}

public Message(Integer code) {
    this.code = code;
}

public Message(Integer code, Object data) {
    this.code = code;
    this.data = data;
}

public Message(String username, String hashedPassword, Integer code,
        String from, String to, Object data) {
    this.username = username;
    this.hashedPassword = hashedPassword;
    this.code = code;
    this.from = from;
    this.to = to;
    this.data = data;
}

public Integer getCode() {
    return code;
}
    //other getters and setters
}

That is the class which object i want to send. Here is the client code:

(It is wirtten just for testing)

public static void main(String[] args) {
        try {
            Socket s = new Socket("localhost", 5656);
            ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream());

            Message m = new Message("user3", "123", 100, "2011-06-11 22:22:22",
                "2011-06-11 22:22:22", "test");
        oos.writeObject(m);
        oos.flush();


        ObjectInputStream ois = new ObjectInputStream(s.getInputStream());
        Message n = (Message) ois.readObject();
        System.out.print(n.toString());

        oos.close();
        ois.close();

    } catch (Exception e) {
        e.printStackTrace();
    }

and here is part of server code (yes it is multithreaded):

package systemZarzadzaniaReporterami.serwer;

import java.beans.ExceptionListener;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.sql.SQLException;

public class ClientHandler extends Thread {

private ExceptionListener exceptionListener;

private Socket socket;
private String ip;

public ClientHandler(ExceptionListener exceptionListener, Socket socket) {
    this.exceptionListener = exceptionListener;
    this.socket = socket;
    ip = socket.getInetAddress().getHostAddress();
}

public void run() {
    Message in = null;
    ObjectInputStream inputStream = null;
    ObjectOutputStream outputStream = null;

    try {
    //  ClientListener.CONNECTION_COUNTER++;

        inputStream = new ObjectInputStream(socket.getInputStream());

        in = (Message) inputStream.readObject();
        MessageProcessor messageProcessor = new MessageProcessor();

        System.out.print(in.getUsername());

        outputStream = new ObjectOutputStream(socket.getOutputStream());

        Message out = messageProcessor.process(in, ip);
                System.out.print(in.getCode().toString());      
        outputStream.writeObject(out);
        outputStream.flush();
    } catch (IOException e) {
        e.printStackTrace();
        exceptionListener.exceptionThrown(new ServerException(Codes.ERR_CONNECTION_ERROR));
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (SQLException e) {
        e.printStackTrace();
        exceptionListener.exceptionThrown(new ServerException(Codes.ERR_MYSQL_ERROR));
    } finally {
        if (inputStream != null)
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
                exceptionListener.exceptionThrown(new ServerException(Codes.ERR_CONNECTION_ERROR));
            }
        if (outputStream != null)
            try {
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
                exceptionListener.exceptionThrown(new ServerException(Codes.ERR_CONNECTION_ERROR));
            }
        if (socket != null)
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
                exceptionListener.exceptionThrown(new ServerException(Codes.ERR_CONNECTION_ERROR));
            }
    //  ClientListener.CONNECTION_COUNTER--;
    }
}

}

I am pretty confused now. Because I have no idea why is such thing happening.

What is most important in my opinion that the following code does well when I replace Message with string.

Was it helpful?

Solution

the only difference is the package name

That won't work. The class name (including the package name) must be the same on both sides.

OTHER TIPS

the only difference is the package name

Then they are not the same class. Make sure you have exactly the same class on both sides. That means same package.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top