0

Recently I learn the java program language.I am curious about the java serializable and have a little doubts about it.

Can java serializable objects pass between different classloaders? What's the theory?

WeiDian_Jake
  • 55
  • 1
  • 1
  • 7
  • Could you please rephrase your question. Passing objects between classloaders is possible with all objects, you just get Exceptions when accessing unknown classes. If this is about how to code for communication between classes in diferent classloaders serialization is not the answer. – Espen Brekke Oct 30 '15 at 14:25

2 Answers2

2

Just by implementing the Serializable interface won't allow you to "pass between different classloaders" . You need to write code to persist the serialized object to disk, and then on the other classloader (process) deserialize it. here's an example, taken from http://www.javapractices.com/topic/TopicAction.do?Id=57:

    Car car = new Car();
    ....

    //serialize an object called it car to a file called car.ser.
    try (
      OutputStream file = new FileOutputStream("car.ser");
      OutputStream buffer = new BufferedOutputStream(file);
      ObjectOutput output = new ObjectOutputStream(buffer);
    ){
      output.writeObject(car); // this call writes file to disk
    }  
    catch(IOException ex){
      logger.log(Level.SEVERE, "Cannot perform output.", ex);
    }

To deserialize the object on the other end/classloader/jvm process you can do this:

 try(
      InputStream file = new FileInputStream("car.ser");
      InputStream buffer = new BufferedInputStream(file);
      ObjectInput input = new ObjectInputStream (buffer);
    ){
      //deserialize the List
      Car car = (Car)input.readObject();
      //display its data
      System.out.println("Recovered Car: " + car);

    }
    catch(ClassNotFoundException ex){
      logger.log(Level.SEVERE, "Cannot perform input. Class not found.", ex);
    }
    catch(IOException ex){
      logger.log(Level.SEVERE, "Cannot perform input.", ex);
    }

EDIT: In order to pickup serialized files and deserialize them, you can use a WatchService

chrisl08
  • 1,658
  • 1
  • 15
  • 24
  • Is it the same that ObjectOutputStream and ObjectInputStream write/read objects as serializable object? – WeiDian_Jake Sep 10 '15 at 08:02
  • If you are asking if they are the same object/class, yes of course they HAVE to be the same class. So you need to place the object "Car" object in the example above in a jar file, and have this jar file in the classpath of both java processes. You also need the objects to have the same serialization version id : http://stackoverflow.com/questions/2258676/what-is-serial-version-id-in-java – chrisl08 Sep 10 '15 at 08:39
  • I want to ask that whether the serializable way is same with ObjectInputStream/ObjectOutputStream reading/writing objects – WeiDian_Jake Sep 10 '15 at 09:40
  • I am not sure what you are asking, but no it is not, the one writes and the other reads serialized objects. – chrisl08 Sep 10 '15 at 11:40
1

A "serializable object" can't, but you can serialize the object, and save the data anywhere. At some future point, on the same machine or a different one, in the same VM or a different one, in the same classloader or a different one, you can deserialize the saved data back into an object.

Andreas
  • 154,647
  • 11
  • 152
  • 247
  • I just make a object implement the Serializable interface.You mean that way can't finish the target that pass object between different classloaders? – WeiDian_Jake Sep 10 '15 at 04:17
  • You can read all about serialization here: https://docs.oracle.com/javase/8/docs/platform/serialization/spec/serialTOC.html – Andreas Sep 10 '15 at 05:14
  • Is it the same that ObjectOutputStream and ObjectInputStream write/read objects as serializable object? – WeiDian_Jake Sep 10 '15 at 08:03