File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Running a .bat/ .cmd file from Java

 
Raghu Devatha
Ranch Hand
Posts: 39
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

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.

Thanks.
 
Rene Larsen
Ranch Hand
Posts: 1179
Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to tell the process which application that your cmd-file should be executed with.

e.g.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47300
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Pie
Posts: 4181
21
IntelliJ IDE Java Python
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 39
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20398
47
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic