Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Reading/Writing with an interactive command via ProcessBuilder  RSS feed

 
Sean Quartz
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greetings,

I'm doing some very basic testing and finding an interesting problem. After hours of Google research and checking around here, I'm still unable to come up with a solution.

Here's some of the testing code I've been playing with:



The ultimate goal is to interactively work the command. WMIC is a Windows command since XP Pro. If you do not pass any parameters, it will keep you in a shell so you can continually execute commands and receive output each time. It seems like no matter what I do, the only way it will work is to close the input stream. I've tried appending some \r\n, \0, \u0000 to the string being passed and flushing but it doesn't work.

What am I missing? This works in PHP but my usage of ProcessBuilder and interactive streams is pretty low. I've spent a few days trying to get this to work as expected.

If I can't get this to work interactively then 1) I'll be sad to walk away without understanding how to do it, 2) I'll have to set it up in a loop to run the command about 40 times to get everything I need. Mostly, #1 is why I'm still working at this.


Thanks!
--Sean
 
K. Tsang
Bartender
Posts: 3628
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the ranch

Let me get this straight. You are trying to read from "wmic" and print (execute) those commands?

Given what you got first focus of the InputStreamReader by commenting all those OutputStream stuff. Once you get this working then put in the OutputStream.

BTW, out.flush() and out.close() should ideally be at the end of the program.
 
Richard Tookey
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
K. Tsang wrote:Welcome to the ranch

Let me get this straight. You are trying to read from "wmic" and print (execute) those commands?

Given what you got first focus of the InputStreamReader by commenting all those OutputStream stuff. Once you get this working then put in the OutputStream.


I don't see this making sense. The OP has already obtained some output so it is working but if the OP does not write his input he may not get any output!


BTW, out.flush() and out.close() should ideally be at the end of the program.


Why ? One needs to flush() the Process stdin stream before anything written to it is guaranteed to reach the invoked processes stdin. Since the OP want to send many commands in an interactive manner to the process a flush() is pretty much needed after every line of input. One needs to close() the Process stdin before one waits for the process to complete or it might never complete and one would have a deadlock!

Though I have lots of experience of using ProcessBuilder and Runtime.exec() I have never used it on "wmic" so I don't know what interactions it has. What I do know is that in general each command to stdin needs to be terminated by an EOL and flush()ed. In general one should use different threads to handle the Process stdin and stdout and one should merge stdout with stderr only if one does not really care about them. Since the OP wants an interactive session then I suspect he needs to process stdout, stderr and stdin in different threads.

I have used ProcessBuilder and Runtime.exec() on both Windows and *Nix systems. In general I find Windows more difficult than *Nix since just about everything one tries to invoke is a 'special case'.

A must read for both ProcessBuilder and Runtime.exec() is the aged but still very relevant (even for ProcessBuilder) http://www.javaworld.com/jw-12-2000/jw-1229-traps.html .


  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!