• 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

Runtime.exec("c.bat")

 
Ranch Hand
Posts: 211
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a problem in running a batch file using runtime.exec();
the problem is if the java file and .bat file is in the same directory the program is working fine.but if the files are in different directory it is not working.also no exceptions are thrown.I am giving the correct path in java also like.Process proc = rt.exec("c://test/ddd.bat");
the exit value for the correct execution is 0 and for other is 1.program is given below.

Pls help
[ December 15, 2003: Message edited by: Michael Ernest ]
 
High Plains Drifter
Posts: 7289
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Shouldn't those be backslashes for a DOS program?
(Shows you how much I know about programming in a M$ environment...)
 
Mathews P Srampikal
Ranch Hand
Posts: 211
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I removed forward slash and made backward slash (double and single)....still not working.
 
Michael Ernest
High Plains Drifter
Posts: 7289
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you tried using this version of exec():

In this invocation, the parameter dir is the working location of the batch file.
I haven't played with this at all in a very long time, so please just take this as a guide to working out the problem yourself. I don't have a Windows machine available to try it myself.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If ddd.bat produces ANY output to standard out or err you MUST consume that output in your program or get strange results - look at the Process Javadocs. This comes up frequently - do a search for exec
Bill
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's a good article from JavaWorld covering common problems. You may find "assuming a command is an executable program" to be useful - try invoking cmd.exe or command.com as shown.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your batch file will start with current directory = the JVM's current default. You might just have to add drive and CD commands to your batch file. It's also worth your while to read the stdout and errout from Process and display any success or error messages. One warning there ... the waitfor() method might return long before the stdout and errout read to end of file, so take care not to close them before they're done! I used separate threads to read the streams.
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Regarding output and error streams, you can save yourself some trouble if you write your batch file to redirect any output to some other file. E.g.
myprog.exe 1>out.txt 2>&1
means send standard output (1) to file out.txt, and send error output (2) to the same place as standard output (1). Or something like that, depending on your OS.
 
Mathews P Srampikal
Ranch Hand
Posts: 211
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot...we got it worked by passing different form of .exec();
Thanks.
reply
    Bookmark Topic Watch Topic
  • New Topic