File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Runtime.exec("c.bat")

 
Mathews P Srampikal
Ranch Hand
Posts: 211
  • 0
  • Mark post as helpful
  • send pies
  • 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 ]
 
Michael Ernest
High Plains Drifter
Sheriff
Posts: 7292
Netbeans IDE VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I removed forward slash and made backward slash (double and single)....still not working.
 
Michael Ernest
High Plains Drifter
Sheriff
Posts: 7292
Netbeans IDE VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13048
6
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot...we got it worked by passing different form of .exec();
Thanks.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic