Inverse serialization of objects error: java.lang.ClassNotFoundException

Source: Internet
Author: User

A bug access to the data is always said class can not find, I carefully read the serialization implementation of the same class name is the same and then on the server to receive the object when the general report this do, online search a lot of the original is the package name is not the same, resulting in two classes are not the same program is really a strict thing ah paste code to see

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)

Hint class can not find, but actually the class file is really real existence, then the Internet search, sure enough to find the answer.

You can refer to the article: http://www.javapractices.com/topic/TopicAction.do?Id=45

The most basic two points:

1) need the same package name

2) the same serialization 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 {
/**
* Welcome to use this source code if you have any questions please add QQ Group: 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;
}


}

Inverse serialization of objects error: java.lang.ClassNotFoundException

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.