• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

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: 79967
396
  • 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: 22815
132
Eclipse IDE Spring 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.
 
That feels good. Thanks. Here's a tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic