使用arraylist< string>作为属性反序列化java对象

9jyewag0  于 2021-06-30  发布在  Java
关注(0)|答案(0)|浏览(175)

我正试图反序列化我的对象,但出现以下错误:

java.lang.ClassNotFoundException: Test$1
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:606)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:168)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
    at java.base/java.lang.Class.forName0(Native Method)
    at java.base/java.lang.Class.forName(Class.java:468)
    at java.base/java.io.ObjectInputStream.resolveClass(ObjectInputStream.java:771)
    at java.base/java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1998)
    at java.base/java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1884)
    at java.base/java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2172)
    at java.base/java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1701)
    at java.base/java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:2510)
    at java.base/java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:2404)
    at java.base/java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2205)
    at java.base/java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1701)
    at java.base/java.io.ObjectInputStream.readObject(ObjectInputStream.java:508)
    at java.base/java.io.ObjectInputStream.readObject(ObjectInputStream.java:466)
    at ClientHandler.run(ClientHandler.java:29)
    at java.base/java.lang.Thread.run(Thread.java:832)

我想要的是将我的对象发送到一个服务器(这样做有效),并且能够在那里使用它(这样做无效)。
这是我的代码(客户):

public class Test extends Thread {

    public static void main(String[] args) {
        ThreadGroup clients;
        ObjectOutputStream outStream;
        ObjectInputStream inStream;

        try {
            Socket s = new Socket("poggivpn.ddns.net", 8189);
            outStream = new ObjectOutputStream(s.getOutputStream());
            inStream = new ObjectInputStream(s.getInputStream());

            ArrayList < String > a = new ArrayList < >() {
                {
                    add("alessandro@gmail.com");
                }
            };
            outStream.writeObject((new RequestSendEmail(new Login("michele@gmail.com", "michele1"), a, "Ciao!", "Come va?")));

            s.close();

        } catch(IOException
        /*|ClassNotFoundException*/
        e) {
            e.printStackTrace();
        }
    }
}

服务器:

public void run() {
    try {
        outStream = new ObjectOutputStream(incoming.getOutputStream());
        inStream = new ObjectInputStream(incoming.getInputStream());

        Object obj = inStream.readObject();
        System.out.println(obj.getClass().toString());
        switch (obj.getClass().toString()) {
        case "class ComunicationObjects.Login":
            Login l = (Login) obj;
            if ((u = Model.authenticate(l.getUserMail(), l.getEncryptedPassword())) != null) outStream.writeObject(new ReplyLogin(u.getName()));
            else outStream.writeObject(new ReplyLogin());
            break;
        case "class ComunicationObjects.RequestSendEmail":
            RequestSendEmail r = (RequestSendEmail) obj;
            System.out.println(r.getSubject());
            break;
        default:
            System.out.println("NO");
        }
    } catch(IOException | ClassNotFoundException e) {
        try {
            e.printStackTrace();
            incoming.close();
        } catch(IOException e1) {
            e1.printStackTrace();
        }
    } finally {
        try {
            incoming.close();
        } catch(IOException e) {
            e.printStackTrace();
        }
    }
}

从对象中删除arraylist一切正常!

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题