• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Executing OS Commands

 
anya
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I was trying to invoke Operating System commands from Java and I can have a simple Java class to do that, but I would like to make use of Java Threads to do this in case of the called exe that doesn't responds for a while and would like to terminate the call. Any help in this would be greatly appreciated.
Thanks
Anil
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've done this with several threads. See if this makes sense:
Start one for the object that does the runtime.exec(). Now your main program doesn't block waiting for the command to finish.
Start another for an object that reads the process stdout, and another for the errout. That way you don't block waiting for the command to write to stdout or errout, which is good because you don't know which one it might write to next, if ever.
It gets a little tricky knowing when the command is truly finished. You can wait for the process, but it can return well before the stdout and errout streams indicate eof. I would up making the object that runs the command a listener for events published by the two stream readers. They publish when they have data and when they hit eof.
You were interested in timing-out the command, I presume to kill it if it takes too long. That can be tricky, too. Try using a java.util.Timer to schedule an event that interrupts the command or maybe all three of the threads. Let me know if that works!
 
Maha Hassan
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear all
can anyone post an example ,like the one Stan James is talking about ,on how to use threads with runtime and to print the output ?
thanks all
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried something on your own? If so, what problems have you encountered? Feel free to post your own code that implements what Stan described above and we can help you figure out the kinks.

Layne
 
Maha Hassan
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i dont have a code
as i am having troubles where to start
i am not familier with threads ..so i was hoping if someone can send me links or examples that can help me
thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic