Call an exe from Java with passing parameters with writing to stdout and reading from stdin

给你一囗甜甜゛ 提交于 2019-11-29 18:09:48

I don't know what the IOUtils class is and what the toString() method is doing. However, a way to read from the InputStream is:

InputStreamReader isr;
BufferedReader br;

String input = "";
String line;

try {

  isr = new InputStreamReader( p.getInputStream );
  br = new BufferedReader( isr );

  while( (line = br.readLine()) != null ) {
    input += line + "\n";
  }

} catch( Exception e ) {
  e.printStackTrace();
}
Emanuele Parisi

If I well understood your problem, you are looking for a popen-equivalent function in Java. Maybe this discussion can help you :)

Java: popen()-like function?

Instead of adding \n in the end of line, stream should be closed thus after replacing

IOUtils.write("1" + System.lineSeparator(), processOutputStream);

with

IOUtils.write("1", processOutputStream);
processOutputStream.close();

Code became work properly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!