aspose file tools*
The moose likes Java in General and the fly likes running external program from Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "running external program from Java" Watch "running external program from Java" New topic
Author

running external program from Java

sabdhagiri manikandan
Greenhorn

Joined: Aug 04, 2010
Posts: 4
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

Joined: Jun 29, 2008
Posts: 125
Is the main thread(Your current java program) exiting or the utility program. Can you please be a bit more specific?

Cheers
Aneesh


Cheers
Aneesh
sabdhagiri manikandan
Greenhorn

Joined: Aug 04, 2010
Posts: 4
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

Joined: Oct 13, 2005
Posts: 36453
    
  15
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

Joined: Jun 01, 2010
Posts: 618
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

Joined: Aug 04, 2010
Posts: 4
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

Joined: Aug 04, 2010
Posts: 4
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

Joined: Oct 13, 2005
Posts: 36453
    
  15
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: running external program from Java
 
Similar Threads
Understanding System.exit
Run a batch file (.bat) in a servlet application through localhost
Launching telnet using java program fails with Win 7 OS
execute exe from cmd line
How to run .cmd from Java