I have a jar file which keeps waiting for user input, process and print out result (in multiple lines) like this
>Input sentence 1
Result 1
Result 2
>Input sentence 2
Result 1
Result 2
Result 3
>
The only way to exit this jar program is by pressing Ctrl + C.
Now, I want to invoke this jar file from my java program. My code looks like this:
processBuilder = new ProcessBuilder(PATH_TO_BIN);
Process process = processBuilder.start();
writer = new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));
reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
writer.write(inputSentence);
writer.newLine();
writer.flush();
String line;
while ((line = reader.readLine()) != null){ //<<<< it hangs here
System.out.println(line);
}
My program can successfully print out the result returned by "jar" program, but it keep hanging at the next reader.readLine()
and I can not provide the next input to "jar" program.
Is there any way to fix this issue?
Thanks in advance!