java 对象反序列化流程,从子流程序列化对象并尝试从父流程ProcessBuilder反序列化时发生java.io.EOFException...

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

解决方案

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值