CE_ CE_ - 3 months ago 27
Java Question

I can't cast my object after sending it with netty

When I read the response in the server, I get the right object but when I try to cast it to Packet, I got this exception :

java.lang.ClassCastException: java.lang.String cannot be cast to server.Packet
at server.JCoincheServerHandler.channelRead(JCoincheServerHandler.java:21)

This is my packet class :

public class Packet implements Serializable
public int code;
int intData;
String data;
String user;

This is how I send it in my client:
The sending is successful, "send failed" is never printed

public void sendMessage(Object object)
ChannelFuture cf = _chan.write(object);
if (!cf.isSuccess()) {
System.out.println("Send failed: " + cf.cause());

This is how I read it in my server:

public void channelRead(ChannelHandlerContext ctx, Object msg) {
Packet packet = (Packet) msg;
System.out.println("Message received");

What did I do wrong ?


new ObjectEncoder(),
new ObjectDecoder(ClassResolvers.cacheDisabled(null)),


Your client must be sending a String.

Also verify that your client and server shares the class Packet and this class should be in the same packet.

For example common.Packet