Getting run time error as below:
Exception in thread "main" java.lang.ClassCastException: class Message cannot be cast to class javax.crypto.SealedObject (Message is in unnamed module of loader 'app'; javax.crypto.SealedObject is in module java.base of loader 'bootstrap')
at Server.listStoreCheckDelete(Server.java:93)
at Server.main(Server.java:69)
import java.io.*;
import java.net.*;
import java.util.*;
import java.security.*;
import javax.crypto.*;
class Message implements Serializable {
public String recipientHash;
public Date timestamp;
public byte[] key;
public byte[] iv;
public byte[] encryptedMsg;
public byte[] signature;
}
class Server{
static Map<String, String> map;
static List<String> list;
static String currentUserid;
public static void main(String [] args) throws Exception {
int port = Integer.parseInt(args[0]);
ServerSocket ss = new ServerSocket(port);
map = new HashMap<>();
list=new ArrayList<String>();
while(true)
{
int firstexe=1;
System.out.println("Waiting rebellions connection...");
Socket s = ss.accept();
DataInputStream dis = new DataInputStream(s.getInputStream());
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
String rebellionMsg=null;
Server.currentUserid=null;
int numberofmsg=0;
try {
while ((rebellionMsg = dis.readUTF()) != null) {
System.out.println(rebellionMsg);
if (firstexe==1)
{
firstexe=0;
dos.writeUTF("Hey! You have "+numberofmsg+" for you!");
dos.flush();
}
listStoreCheckDelete(rebellionMsg,s);
}
}
catch(IOException e) {
System.err.println("Client closed its connection. -->"+e);
}
}
}
public static void listStoreCheckDelete(String rebellionMsg,Socket s) throws Exception
{
String listAddition=null;
ObjectInputStream in = new ObjectInputStream(s.getInputStream());
SealedObject so = (SealedObject)in.readObject();
}}
Exception in thread "main" java.lang.ClassCastException: class Message cannot be cast to class javax.crypto.SealedObject
SealedObject so = (SealedObject)in.readObject();
You have been sent a serialised object of type Message
, not SealedObject
. The line should read:
Message message = (Message)in.readObject();