• 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

Running a .bat/ .cmd file from Java

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1179
Mac OS X Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to tell the process which application that your cmd-file should be executed with.

e.g.
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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".
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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>


 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic