• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Executing OS Commands

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
F is for finger. Can you stick your finger in your nose? Doesn't that feel nice? Now try this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic