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 ]
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.
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
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
Joined: Jan 30, 2000
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
Joined: Nov 26, 2002
Thanks a lot...we got it worked by passing different form of .exec(); Thanks.