java readobject 方法抛出 ClassNotFoundException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2916107/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 23:26:55  来源:igfitidea点击:

readobject method throws ClassNotFoundException

javaclientclassnotfoundexception

提问by eruina

I'm trying to pick up Java and wanted to test around with Java's client/server to make the client send a simple object of a self defined class(Message) over to the server. The problem was that I kept getting a ClassNotFoundException on the server side.

我正在尝试学习 Java 并希望使用 Java 的客户端/服务器进行测试,以使客户端向服务器发送一个简单的自定义类(消息)对象。问题是我一直在服务器端收到 ClassNotFoundException。

I think the rest of the codes seem to be alright because other objects such as String can go through without problems.

我认为其余的代码似乎没问题,因为其他对象(例如 String)可以毫无问题地通过。

I had two different netbeans projects in different locations for client and server each.

我在客户端和服务器的不同位置有两个不同的 netbeans 项目。

Each of them have their own copy of Message class under their respective packages. Message class implements Serializable.

他们每个人在各自的包下都有自己的 Message 类副本。消息类实现可序列化。

On the client side, I attempt to send a Message object through.

在客户端,我尝试通过发送 Message 对象。

On the server side, upon calling the readObject method, it seems to be finding Message class from the client's package instead of it's own. printStackTrace showed: "java.lang.ClassNotFoundException: client.Message" on the server side

在服务器端,在调用 readObject 方法时,它似乎是从客户端的包而不是它自己的包中找到 Message 类。printStackTrace 在服务器端显示:“java.lang.ClassNotFoundException: client.Message”

I have not even tried to cast or store the object received yet. Is there something I left out?

我什至还没有尝试转换或存储收到的对象。有什么我遗漏了吗?

回答by BalusC

The package name and classname must be exactlythe same at the both sides. I.e. write once, compile once and then give the both sides the samecopy. Don't have separate server.Messageand client.Messageclasses, but a single shared.Messageclass or something like that.

两端的包名和类名必须完全相同。即编写一次,编译一次,然后给双方相同的副本。不要有单独的server.Messageclient.Message类,而是一个shared.Message类或类似的东西。

If you can guarantee the same package/class name, but not always whenever it's exactlythe same copy, then you need to add a serialVersionUIDfield with the same value to the class(es) in question.

如果您可以保证相同的包/类名称,但并非总是完全相同的副本,那么您需要向相关类添加serialVersionUID具有相同值的字段。

package shared;

import java.io.Serializable;

public class Message implements Serializable {
    private static final long serialVersionUID = 1L;

    // ...
}

回答by Daniel

The reason is, that the readObject() in ObjectInputStream is practically implemented as:

原因是 ObjectInputStream 中的 readObject() 实际上实现为:

 String s = readClassName();
 Class c = Class.forName(s); // Here your code breaks
 Object o = c.newInstance();
 ...populate o...