Sorry for this odd name ...
I have the following situation: I want my Java program to interact with an external console. In order to "send" individual commands to this console, I need to simulate what will be "enter the key pressed" on a regular console. To clarify what I want, imagine that mysql did not have another API, and I would need to interact through the console. Although this is not my actual problem, it is fairly close.
I have the following code:
String command = "/usr/local/mysql/bin/mysql"; Process child = Runtime.getRuntime().exec(command); StreamGobbler gobbler = new StreamGobbler(child.getInputStream()); gobbler.start(); BufferedWriter out = new BufferedWriter(new OutputStreamWriter(child.getOutputStream())); out.write("help");
If the out.close() call is made, everything is fine. But, of course, in this way I can send only one command, which I do not want. But if out.close() omitted, another program never executes the command. I assume that he is still waiting for the command to βfinishβ what will be done on the regular console by pressing enter. out.write(System.getProperty("line.separator")); and out.newLine(); (which is the same) do not solve the problem, and out.write("\r\n"); and out.write((char) 26); (EOF).
Of course, maybe I'm doing it completely wrong (i.e. the wrong approach). Then I would appreciate a pointer in the right direction ...
Any help on this is much appreciated.
java process interface
roesslerj
source share