File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Runtime.exec( Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Runtime.exec("c.bat")" Watch "Runtime.exec("c.bat")" New topic
Author

Runtime.exec("c.bat")

Mathews P Srampikal
Ranch Hand

Joined: Nov 26, 2002
Posts: 211
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 ]

Thanks,
Mathews
Michael Ernest
High Plains Drifter
Sheriff

Joined: Oct 25, 2000
Posts: 7292

Shouldn't those be backslashes for a DOS program?
(Shows you how much I know about programming in a M$ environment...)


Make visible what, without you, might perhaps never have been seen.
- Robert Bresson
Mathews P Srampikal
Ranch Hand

Joined: Nov 26, 2002
Posts: 211
I removed forward slash and made backward slash (double and single)....still not working.
Michael Ernest
High Plains Drifter
Sheriff

Joined: Oct 25, 2000
Posts: 7292

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

Joined: Mar 22, 2000
Posts: 12761
    
    5
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

Joined: Jan 30, 2000
Posts: 18671
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.


"I'm not back." - Bill Harding, Twister
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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

Joined: Nov 26, 2002
Posts: 211
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
 
subject: Runtime.exec("c.bat")
 
Similar Threads
running a batch file from java
Running unix command using java shows error
Controlling Windows Services from a Java App
how can i use shell32 in my java program
Runtime.exec problem