gpt4 book ai didi

从流中读取对象时出现 Java ClassNotFoundException

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:22:27 25 4
gpt4 key购买 nike

我在从 ObjectInputStream 读取对象时收到 ClassNotFoundException。正在发送的对象是读取代码引用的“WorkUnit”的子类。看起来接收端在提示,因为它不知 Prop 体的实现。当我只引用对象的父类(super class)时,为什么它会关心这一点:接收方的“WorkUnit”?

从流中读取的代码:

private Object readObject() {
Object object = null;
try {
object = objectIn.readObject();
} catch (SocketException | EOFException e) {
// Socket was forcedly closed. Probably means client was
// disconnected
System.out.println("[NetworkHandler] SOCKET CLOSED");
shouldContinue = false;
if (!isClient)
server.clientDisconnected(clientID);
} catch (ClassNotFoundException | IOException e) {
// If shouldContinue is true, we haven't said that we want to close
// the connection
if (shouldContinue) {
e.printStackTrace();
System.err
.println("[NetworkHandler] Error: Couldn't read object correctly");
}
}
return object;
}

工作单元实现:

import java.util.LinkedList;
import java.util.List;

import Application.WorkUnit;


public class WorkUnitImplementation extends WorkUnit<Integer, Integer> {

private static final int INPUT_LENGTH = 1000;

public WorkUnitImplementation() {
super();

setInputLength(INPUT_LENGTH);
}

@Override
public Integer doWork(Integer input) {
wait(50);
return (input % 2 == 1) ? input : null;
}

@Override
public List<Integer> inputFactory(int begin, int end) {
List<Integer> result = new LinkedList<>();
for (int i = begin; i < end; i++) {
result.add(i);
}
return result;
}

private void wait(int time) {
try {
Thread.sleep(time);
} catch (Exception e) {

}
}
}

发送工作单元的代码:

    public void uploadWorkUnit(WorkUnit workUnit) {
try {
objectOut.writeObject(new UploadWorkUnit(workUnit));
objectOut.flush();
System.out.println("[NetworkHandler] Uploaded workUnit");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

异常跟踪:

java.lang.ClassNotFoundException: WorkUnitImplementation
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at java.io.ObjectInputStream.resolveClass(ObjectInputStream.java:622)
at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1593)
at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1514)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1750)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1347)
at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1964)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1888)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1771)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1347)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:369)
at Networking.NetworkHandler.readObject(NetworkHandler.java:188)
at Networking.NetworkHandler.run(NetworkHandler.java:90)
at java.lang.Thread.run(Thread.java:722)

[NetworkHandler] 错误:无法正确读取对象

最佳答案

It would seem that the receiving end is complaining because it doesn't know about the specific implementation. Why would it care about that when I'm only referencing the object's superclass: "WorkUnit" on the side that is receiving?

这是因为流由实现类的字段组成。

来自 ObjectOutputStream文档:

The default serialization mechanism for an object writes the class of the object, the class signature, and the values of all non-transient and non-static fields.

反序列化过程读取流以发现应该自动重构的类。它尝试创建该类的一个新实例,以便它可以用流中包含的字段填充它。

如果接收端没有编写的相同类,它将无法工作——您将得到一个 ClassNotFoundException

(也就是说,被序列化的类可以使用 writeReplace(),但这可能超出了这个问题的范围)。

关于从流中读取对象时出现 Java ClassNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10479481/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com