serialization objects in java -


is there way around during serialization of object full match / repetition of way? example:

on client -

com.example.myprojectone.model.myclass

on server side -

com.example.notmyproject.entity.myclass

i -

java.lang.classnotfoundexception: com.example.myprojectone.model.myclass

with full coincidence of names of work packages

public class server implements runnable {  private settingsconnection settingsconnection; private onreceiveobject onreceiveobject; private serializer serializer;  /**  * @param remoteserveraddress - address remote server  * @param inputport - port on server  * @param outputport - port used send  * @param password - password should same on client side , server side  * @param handler - defines name of method, should called, after received data on server side  */ public server(string remoteserveraddress, int inputport, int outputport, string password, onreceiveobject handler) {     settingsconnection = new settingsconnection();     settingsconnection.setaddressremoteserver(remoteserveraddress);     settingsconnection.setinputport(inputport);     settingsconnection.setoutputport(outputport);     this.onreceiveobject = handler;     serializer = new serializer();     new thread(this).start(); }  public void senddata(serializable object, string callbackfunction) {      container container = new container();      try {         container.setdata(serializer.serialize(object), container.getinitvector()));         container.sethandler(callbackfunction);         inetaddress ipaddress = inetaddress.getbyname(settingsconnection.getaddressremoteserver());          try (socket socketconnectiontosever = new socket(ipaddress, settingsconnection.getoutputport())) {             outputstream outputstream = socketconnectiontosever.getoutputstream();             if (outputstream != null) {                 outputstream.write(serializer.serialize(container));                 outputstream.flush();             }         } catch (ioexception e) {             e.printstacktrace();         }     } catch (ioexception e) {         e.printstacktrace();     } }  @override public void run() {      final executorservice asynctakescode = executors.newcachedthreadpool();      runnable threadtaskserver = new runnable() {         @override         public void run() {             try {                 serversocket serversocket = new serversocket(settingsconnection.getinputport());                 while (true) {                     socket connectionsocketclient = serversocket.accept();                     asynctakescode.submit(new threadtaskclient(connectionsocketclient));                 }             } catch (ioexception e) {                 system.err.println("unable process client request");                 e.printstacktrace();             }         }     };      thread threadserver = new thread(threadtaskserver);     threadserver.start(); }  private class threadtaskclient implements runnable {     private final socket connectionsocketclient;      private threadtaskclient(socket connectionsocketclient) {         this.connectionsocketclient = connectionsocketclient;     }      @override     public void run() {          inputstream inputstream = null;         try {             inputstream = connectionsocketclient.getinputstream();         } catch (ioexception e1) {             e1.printstacktrace();         }          try {             object o = serializer.deserialize(ioutils.readfully(inputstream, -1, false));             if (o instanceof container) {                 container container = (container) o;                 serializable remoteobject = (serializable) serializer.deserialize(container.getdata()));                 string callbackfunction = container.gethandler();                 onreceiveobject.processremoteobject(remoteobject, callbackfunction);             }         } catch (ioexception | classnotfoundexception e) {             e.printstacktrace();         } {             try {                 connectionsocketclient.close();             } catch (ioexception e) {                 e.printstacktrace();             }         }     } 

if you're using java's serialization, classes must match both ends.

include class in server side's classpath.

if can't that, make work creating alias of class name other class:

package com.example.myprojectone.model;  public class myclass extends com.example.notmyproject.entity.myclass { } 

there few caveats of course, example work if 2 classes share same non-transient instance field names , types.


Comments

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

java - How to resolve The method toString() in the type Object is not applicable for the arguments (InputStream) -