0

I am trying to run an interactive executable from Java application using ProcessBuilder; it's supposed to take input, produce output and then wait for the next input. The main problem here with Input/Output streams. I send an input and get nothing. Here is the code:

private static Process process;
private static BufferedReader result;
private static PrintWriter input;

process = new ProcessBuilder("compile-lm", lmFile.toString(), " --score yes").redirectErrorStream(true).start();

input = new PrintWriter(new OutputStreamWriter(process.getOutputStream()), true);
input.println(message);
System.out.println(message);

result = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = new String();

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

/* Some processing for the read line */

System.out.println("output:\t" + line);
}
4
  • if you have an exit status try to get it by using int es=process.waitFor(); and displaying it to se what is the problem Commented Aug 1, 2012 at 9:49
  • @ZiedJaballah I tried to get the exit status after the right operation 'input.println(message)' and it was 0 "normal termination". Is there anything else i can try to investigate where is the problem? Commented Aug 1, 2012 at 10:43
  • try to change the command. the code sounds clean. maybe you are not getting an output just because the commande returns nothing. Commented Aug 3, 2012 at 17:09
  • I figured out that ProcessBuilder runs the command once and then closes the executable although it has to be running (interactive). That's why I didn't get any results because it's shutdown. Could you advise what else to try? Commented Aug 7, 2012 at 11:03

3 Answers 3

1

I have tried your code it works fine there is no problem with the code. I think that the problem with the command that you are trying to execute ( it returns nothing ). try to change parameters or even change the entire command to test. and if you can execute the comand in other place ( terminal for example try it and see the output with the same parameters )

Sign up to request clarification or add additional context in comments.

Comments

0

I have used a similar setup many times over but can not find a working copy right now :( My first instinct though is to move the line where you initialise the reader (result variable) to before the one where you send the command out to the process (input.println(message)).

1 Comment

Why would that make any difference?
0

Try closing the output stream to the process. Basically you're at the mercy of whatever buffering is happening in the output side of the child process.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.