简体   繁体   中英

Deserilization in java,in google app engine

I have a deserialization problem in Java, in Google App Engine. I want to send a my_class object which is serialized. Although no error is reported on the sending side, the receiving side throws a ClassNotFoundException on this code:

my_class obj = (my_class) out.readObject();

in server code.....

The exception message is showing the client-side my_Class name in a path-like form. I also copied my_class file in backend module of appengine, but still I get this error. Googling points to something like a class loading problem, but I don't understand quite what that is or how to fix it.

my code in android studio client side

URL url = new URL(urln);
s1=s1+" two";
con=(HttpURLConnection)url.openConnection();
con.setRequestMethod("POST");
con.setDoInput(true);
con.setDoOutput(true);
ObjectOutputStream oos=new ObjectOutputStream(new BufferedOutputStream(con.getOutputStream()));
oos.reset();
oos.writeObject(objectofserializable_my_class);
oos.flush();
oos.close();
DataInputStream dis=new DataInputStream(new BufferedInputStream(con.getInputStream()));
s3=dis.readLine();

which is working fine i am getting http response ie, e.getMessage() exactly com.package.my_class but i copied exact my_class.java file in backend module,google appengine but with different package statement in the receiver side...

server side code on servlet appengine

public class ServerSide extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
try {
ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(req.getInputStream()));
my_class obj=(my_class)ois.readObject();
String s1=obj.getName();
s=" i got it ";
resp.setContentType("text/plain");
resp.getWriter().println(s);
} catch (ClassNotFoundException e) {
resp.getWriter().print(e.getMessage());
} catch(IOException e){
resp.getWriter().print(e.getMessage());
}
}
}

Sorry i can't get printstacktrace because it is running in Google App engine......

To successfully transfer an object by serialization / deserialization, both parties to the communication must be able to load compatible versions of the class of the object being transferred, and of the classes of any objects accessible via the serialized object (these are transferred, too). Loading those classes often requires loading other classes, as well. An object is "accessible via" another object if that other object holds a reference to it in a member variable, directly or indirectly.

The source line you presented refers to class my_class , therefore any class whose source contains that line could not itself be loaded without some version of class my_class being loaded as well. Inasmuch as you have attributed the exception to a specific line, I suppose the receiving program actually ran (and therefore first loaded successfully), and that the exception's stack trace identified the line you presented.

In that case, it cannot be that the program throws a ClassNotFoundException pertaining to class my_class , as it must have not only found that class, but loaded it already. I could believe an InvalidClassException , which could arise for a couple of different reasons. If the exception is in fact a ClassNotFoundException , however, then it must pertain to a different class. If my_class declares any nested classes, then perhaps one of those is the one that cannot be found (and this is one reason why you should always include the complete exception message and stack trace in your question). The JVM-format name of such a class would be something like me/mypackage/my_class$Nested .

Fix such an exception by putting the needed class file in the receiving program's class path.

Ohhhhh god i finally figured it. The package statement of my_class file is different in sender and receiver side so the error ClassNotFoundException . somehow after managing same package statement in my_class java file in sender and receiver side.This got clear way out......... ie, Exact versions of serializable class should be present in both sender and receiver side...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM