Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Pass Value to Running Program  RSS feed

 
Arthur Buliva
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi friends. I am developing an application to automatically monitors SMS'es from my Linux box running GNOKII. The reading part of the texts is completed. What remains is the sending part. This is what I mean.

From konsole, if I do (with XXXX being the number), GNOKII replies by asking me to enter the number and terminate by <cr> then <ctrd-d>. I can get up to the part of using runtime like
but I am stuck on how to input the text, then send <cr> then <ctrl-d> to the process. Anyone?

Thanks.
 
Joe Ess
Bartender
Posts: 9428
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Runtime.exec() returns a Process instance. If you look at the documentation, you'll see that you can get an output stream which you can use to write your commands. It is also wise to start a thread to read the input stream which contains the program's output. If you don't the process's output buffer can fill up and the process will stop responding.
 
Arthur Buliva
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am able to pipe the output of the program to a JTextArea just fine. But the output waits for me to supply the text and press <cr> then <ctrl-d> as I can see by the output stopping. This is where the problem is. how to supply the text then supply press <cr> then <ctrl-d> to the output.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, the OutputStream that you get from the Process is the standard input of the spawned process. Therefore, it is to this OutputStream that you should write the input.

If you are using a JTextArea, stuff you type into the JTextArea will not magically get sent to the spawned Process. You'd have to hook into the JTextArea (I'm not a Swing expert, so don't know exactly how) and send the typed characters to the OutputStream.
 
Arthur Buliva
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is so far as I have managed to get:



Let me explain the code;

I have managed to query the JVM for the keycode for the enter key, which it returned as 13. So I did the gnokii --sendsms thing then sent the message followed by the keycode of the enter key. Now what puzzles me is that of the ctrl-d part, which i (wrongly) guessed as 17. Anyone?
 
Arthur Buliva
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have managed after a couple of guessworks to find the code. Here is the code snippet that works

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!