java.io.EOFException从子进程序列化对象并尝试从父进程ProcessBuilder反序列化时

时间:2018-05-20 19:04:15

标签: java serialization deserialization processbuilder

我正在使用java ProcessBuider创建一个新进程,我想要一个对象从创建子进程发送给父进程。在这里,我从子端序列化对象并将其发送给父对象。但是当我从父母那里读取发送的对象时,有一个例外

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

当我尝试阅读时,感觉仍然没有收到父流。Parent, Child和发送对象java文件详述如下。

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();
    }
}

我在这里做错了什么?任何解决此问题的建议

1 个答案:

答案 0 :(得分:0)

当您从父级删除{ "status": "SUCCESS", "message": "something", "data": { "trade_origin_iso3country": "GBR", "countries": { "ARM ": "Armenia", "BLR": "Belarus ", "DNK": "Denmark", "GBR": "United Kingdom", "MCO": "Monaco" } } } 代码段时,这将开始工作,其中删除表示子级和父级将不再共享公共标准I / O.子级和父级都将具有不同的标准I / O