简体   繁体   English

readobject方法抛出ClassNotFoundException

[英]readobject method throws ClassNotFoundException

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. 我正在尝试使用Java,并想通过Java的客户端/服务器进行测试,以使客户端向服务器发送自定义类(消息)的简单对象。 The problem was that I kept getting a ClassNotFoundException on the server side. 问题是我一直在服务器端收到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类副本。 Message class implements Serializable. 消息类实现了Serializable。

On the client side, I attempt to send a Message object through. 在客户端,我尝试通过发送消息对象。

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. 在服务器端,调用readObject方法时,似乎是从客户端程序包而不是其自己的包中查找Message类。 printStackTrace showed: "java.lang.ClassNotFoundException: client.Message" on the server side 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? 我有什么遗漏的吗?

The package name and classname must be exactly the same at the both sides. 软件包名称和类名称的两侧必须完全相同 Ie write once, compile once and then give the both sides the same copy. 即写一次,编译一次,然后给双方相同的副本。 Don't have separate server.Message and client.Message classes, but a single shared.Message class or something like that. 没有单独的server.Messageclient.Message类,但是没有一个shared.Message类或类似的东西。

If you can guarantee the same package/class name, but not always whenever it's exactly the same copy, then you need to add a serialVersionUID field 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;

    // ...
}

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...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用readObject()方法的ClassNotFoundException - ClassNotFoundException with readObject() method objectInputstream的readObject方法在更改类包时抛出ClassNotFoundException - readObject method of objectinputstream throw ClassNotFoundException when changed class package ObjectInputStream readObject():ClassNotFoundException - ObjectInputStream readObject(): ClassNotFoundException ObjectInputStream.readObject()中的ClassNotFoundException - ClassNotFoundException in ObjectInputStream.readObject() ReadObject方法 - ReadObject Method 来自ObjectInputStream的readObject时发生ClassNotFoundException - ClassNotFoundException while readObject from ObjectInputStream 调用ObjectInputStream.readObject(); 从线程run()调用时,java中的方法抛出ClassNotFoundException; - Invoking ObjectInputStream.readObject(); method in java is throwing ClassNotFoundException when called from thread run(); Java ObjectInputStream readObject引发NullPointerException - Java ObjectInputStream readObject Throws NullPointerException 当我在ObjectInputStream上调用readObject()时发生ClassNotFoundException - ClassNotFoundException when I call readObject() on a ObjectInputStream 如何在ObjectInputStream.readObject中绕过ClassNotFoundException? - How to bypass ClassNotFoundException while ObjectInputStream.readObject?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM