Home » Java » java.io.EOFException when serialize object from child process and try to de-serialize from parent process ProcessBuilder

java.io.EOFException when serialize object from child process and try to de-serialize from parent process ProcessBuilder

Posted by: admin June 30, 2018 Leave a comment

Questions:

I am creating a new process using java ProcessBuider and I want an object to be sent to the parent from the creating child. Here, I serialize the object from child side and send it to the parent. But when I read the sent object from parent, there is an exception saying

java.io.EOFException
    at java.io.ObjectInputStream$PeekInputStream.readFully(Unknown Source)

Felt like still there are no streams receive to the parent when I am trying to read that.Parent, Child and the Sending object java files are detailed below.

DTO.java

public class DTO implements Serializable{
    private static final long serialVersionUID = 1L;
    private String name;

    public DTO(String name)
    {
        this.name = name;
    }

    public String getName() {
        return name;
    }

@Override
    public int hashCode() {}

@Override
public boolean equals(Object obj) {}

Parent.java

public class Parent {

  public static void main(String[] args) {

      try {
          new Parent().start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

      public void start() throws IOException, InterruptedException, ClassNotFoundException
      {
            String classpath = System.getProperty("java.class.path");
            String className = Child.class.getCanonicalName();

            ProcessBuilder builder = new ProcessBuilder(
                "java", "-cp", classpath, className);

            builder.inheritIO();
            Process process = builder.start();

            if (process.isAlive()) {

                ObjectInputStream input = new ObjectInputStream(process.getInputStream());
                DTO dto = (DTO)input.readObject();
                System.out.println();

            }
      }
}

Child.java

public class Child {

    public static void main(String[] args) throws IOException {
        DTO dto = new DTO("text");

        ObjectOutputStream stream = new ObjectOutputStream(System.out);
        stream.writeObject(dto);
        stream.flush();
        stream.close();
    }
}

What is the wrong that I am doing here ? Any suggestions to fix this

Answers: