对象逆序列化报错:java.lang.ClassNotFoundException
一个bug接受到的数据总是说类找不到,
java.lang.ClassNotFoundException: xxxxxxxxxxxx
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.readObject(ObjectInputStream.java:369)
at xxxxxxxxxxxxxxxxx(TestMetadata.java:103)
提示类找不到,但实际上类文件是确确实实存在的,那就上网搜,果然找到答案。
可以参考文章: http://www.javapractices.com/topic/TopicAction.do?Id=45
最基本的两点:
1) 需要同样的包名
2) 同样的序列化ID
package testServerAndClient;
import java.io.IOException;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
public class client {
/**
* 欢迎大家使用这个源码 如有疑问请加qq群:151648295
*
*/
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new client();
}
public client(){
try {
Socket s=new Socket("127.0.0.1",9999);
ObjectOutputStream oos=new ObjectOutputStream(s.getOutputStream());
User u=new User();
u.setName("xiangqiao123");
u.setPass("123");
oos.writeObject(u);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package testServerAndClient;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) {
new Server();
}
public Server(){
try {
ServerSocket ss=new ServerSocket(9999);
Socket s=ss.accept();
ObjectInputStream ois= new ObjectInputStream(s.getInputStream());
User user=(User)ois.readObject();
System.out.println(user);
} catch (Exception e) {
e.printStackTrace();
}
}
}
package testServerAndClient;
import java.io.Serializable;
public class User implements Serializable {
private String name;
private String pass;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPass() {
return pass;
}
public void setPass(String pass) {
this.pass = pass;
}
}
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。