Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

running external program from Java

 
sabdhagiri manikandan
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I want to run an external utility program from command line and I used the Runtime.getRuntime().exec("cmd string") but the process is exiting after some time how do I make this process to wait until the utility program is being terminated


BUt when I run the utility program directly from the cmd prompt it is working fine...


please give me pointers on what has to be done...


Thanks
 
Aneesh Vijendran
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is the main thread(Your current java program) exiting or the utility program. Can you please be a bit more specific?

Cheers
Aneesh
 
sabdhagiri manikandan
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not the main ... only the utility program, the process exits


Process ps = Runtime.getRuntime().exec("cmd /c myexe.exe args");

this is the code I have used


Thanks
 
Campbell Ritchie
Sheriff
Posts: 48415
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

There are two Streams associated with a Process, which must be "emptied". Never try Runtime.exec() until you have read Michael Daconta's article "when Runtime.exec() won't".
 
Tom Reilly
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Process ps = Runtime.getRuntime().exec("cmd /c myexe.exe args");
Just a guess, did you really want to pass the string "args" to the external program of did you want to pass the contents of a variable args?
 
sabdhagiri manikandan
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

Thanks for taking time in helping me out.


Just a guess, did you really want to pass the string "args" to the external program of did you want to pass the contents of a variable args?



Let me explain what I am trying to accomplish,

I have a executable which will Transcribe the Speech to text and I have to pass two parameters the first one is Filename to which the text has to be written and the second is Status.

and I am running the process using Runtime.exec() it seems that it is working fine for sometime and it is Transcribing the Speech to text and writing it in file but it stops after sometime, one Interesting thing I've noticed here is that it stops writing to file after every 488 - 491 byte chunks.

while going through,
Michael Daconta's article "when Runtime.exec() won't


I came across this line


Because some native platforms only provide limited buffer size for standard input and output streams, failure to promptly write the input stream or read the output stream of the subprocess may cause the subprocess to block, and even deadlock.


is this got anything to do with the execution pattern ?

Since I am new to programming, I don't know where I am going wrong

Thanks








 
sabdhagiri manikandan
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

Thanks for spending time to help me.

with the pointers from Campbell Ritchie, and from the Article I figured out the problem, that I was not handling the output stream of the process.

After handling it it was just working perfect .

and btw I read up on ProcessBuilder which gives you lot of customization options on the environment which Runtime doesn't. which is a cool thing.

but even then we have to be careful about handling the process' error and output stream in separate threads

Thanks
 
Campbell Ritchie
Sheriff
Posts: 48415
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sabdhagiri manikandan wrote:Hi All,

Thanks
You're welcome
is this got anything to do with the execution pattern ?
Don't know, but probably not.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic