How to write "enter key pressed" into the stream?

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"); // here enter key needs to be pressed out.flush(); // out.close(); 

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.

+6
java process interface
source share
3 answers

The following code works fine on both Windows 7 using Java 1.6.0_23 and Ubuntu 8.04 using Java 1.6.0_22:

 public class Laj { private static class ReadingThread extends Thread { private final InputStream inputStream; private final String name; public ReadingThread(InputStream inputStream, String name) { this.inputStream = inputStream; this.name = name; } public void run() { try { BufferedReader in = new BufferedReader( new InputStreamReader(inputStream)); for (String s = in.readLine(); s != null; s = in.readLine()) { System.console().writer().println(name + ": " + s); } } catch (Exception e) { e.printStackTrace(); } } } public static void main(String[] args) throws Exception { String command = "psql -U archadm arch"; final Process child = Runtime.getRuntime().exec(command); new ReadingThread(child.getInputStream(), "out").start(); new ReadingThread(child.getErrorStream(), "err").start(); BufferedWriter out = new BufferedWriter( new OutputStreamWriter(child.getOutputStream())); out.write("\\h"); out.newLine(); out.flush(); out.write("\\q"); out.newLine(); out.flush(); } } 

newLine () is the same as the platform line separator entry. As expected, it prints the help preceding "out:" and then exits. If I did not send "\ q", it will not exit (obviously), but it will print the help anyway. Using "\ r \ n" or "\ r" instead of the platform line separator does not seem to me a good idea, because such command line utilities usually find that they do not receive input from the terminal and accept it in the original text format (think " psql <script.sql "). Good software must properly identify and accept all reasonable leaf strings.

+7
source share

What about out.write((char) 13) ? See Wikipedia Article. I don't have enough code to check this out for you.

0
source share

You can also try looking at this API.

http://download.oracle.com/javase/6/docs/api/java/io/Console.html

In my experience, I have never tried to do anything more than start a single process from the Process API. It looks like you want to enter some commands, I think this API can allow you to do this.

EDIT: Found a tutorial to help you further. http://download.oracle.com/javase/tutorial/essential/io/cl.html

Hope this helps,

0
source share

All Articles