java c 通信,通过Java与C ++进程进行通信

First, I saw a few Q's about this issue in the site, but didn't see any answer that solve my problem.

I have a program written in Java and it calls a cmd program written in C++. (this is an assumption since I don't have the actual source) I know the expected I/O of the C++ program, in the cmd it is two lines of output and then it waits for string input.

I know that the first output line of the program is through error stream, and I receive it properly (this is expected), but I don't get the second line in error or input stream.

I tried to write to the program right after the first line ( the error line) and didn't got stuck, but there was no response.

I tried using 3 different threads, for each stream, but again, nothing was received in input/error stream after the first line, and the program didn't respond to writing through output stream.

My initializers are:

Process p = Runtime.getRuntime().exec("c:\\my_prog.exe");

BufferedReader err = new BufferedReader(new InputStreamReader(p.getErrorStream()));

BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));

BufferedWriter output = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));

Is it possible at all or maybe it depends on the C++ program?

Thanks,

Binyamin

解决方案

Here is how I execute any command line in Java. This command line may execute any program:

private String executionCommandLine(final String cmd) {

StringBuilder returnContent = new StringBuilder();

Process pr;

try {

Runtime rt = Runtime.getRuntime();

pr = rt.exec(cmd);

BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream()));

String line = null;

while ((line = input.readLine()) != null) {

returnContent.append(line);

}

input.close();

LOG.debug(returnContent.toString());

// return the exit code

pr.waitFor();

} catch (IOException e) {

LOG.error(e.getMessage());

returnContent = new StringBuilder();

} catch (InterruptedException e) {

LOG.error(e.getMessage());

returnContent = new StringBuilder();

}

return returnContent.toString();

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值