File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Running a .bat/ .cmd  file  from Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Running a .bat/ .cmd  file  from Java" Watch "Running a .bat/ .cmd  file  from Java" New topic

Running a .bat/ .cmd file from Java

Raghu Devatha
Ranch Hand

Joined: Feb 10, 2009
Posts: 39

I wanted to run a .cmd file from Java. I have something which works for me. Can someone help me understand possible failures of my program.

Is my solution reliable? How can I make sure that once the .cmd is execute there is no processes hanging around.


Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

You need to tell the process which application that your cmd-file should be executed with.


Regards, Rene Larsen
Dropbox Invite
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46337
Raghu Devatha wrote:Is my solution reliable?
No. You need to "empty" the two Streams. Google for the classic article by Michael Daconta: Michael Daconta When Runtime.exec() won't, and follow what it says there. You can simplify the exercise slightly with the ProcessBuilder class, but follow Daconta's examples otherwise.

Moving as too difficult for "beginning".
Steve Luke

Joined: Jan 28, 2003
Posts: 4181

A good article to read is this one from Daconta:
When Runtime.exec() won't

Read through it and understand both what the Runtime.exec() is and is not, but also make sure you understand (and implements) the parts about consuming the output and error streams - failing to consume both (at the same time) can lead to the command stalling as it waits for buffers to free up.

<dern. Too Slow>

Raghu Devatha
Ranch Hand

Joined: Feb 10, 2009
Posts: 39
Thanks for your suggestions. I read the article by Michel Daconta and understood most of it. The problem I am facing now with the approach suggested in the article is getting the exit value of the .cmd file.

I should have mentioned what actually I was doing there. I wanted to export a database table as a csv file. This is what I have done.

When I call the .cmd from Java, I dont get any exit code: When I run the Java class it hangs after printing <ERROR>

Rob Spoor

Joined: Oct 27, 2005
Posts: 20271

That's because you're only reading from the error stream. The process' output buffer is probably full, so you'll need to start reading from the Process' input stream as well. That's what the article is all about. And because you can't read from two streams simultaneously in one thread you'll need to use a new thread for the input stream. That, or use ProcessBuilder and redirect the error stream so you can read everything from the input stream.

How To Ask Questions How To Answer Questions
I agree. Here's the link:
subject: Running a .bat/ .cmd file from Java
It's not a secret anymore!