Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
338 views
in Technique[技术] by (71.8m points)

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This starts working when you remove the builder.inheritIO(); code segment from parent where removing that indicates both child and parent will no longer shared a common standard I/O. Both child and parent will have different standard I/Os


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...