Running a .bat file with runtime.exec()
My goal is to have a class that will run and batch file and allow my program to continue running.
I have been over http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html
And I am actually using an example to run the bat file. And it works. The only problem is that for example the batch file contains "notepad" notepad will open but my program will stop until the opened notepad is closed. Then it carries on as desired.
Any ideas how this code could be modified to simply start the batch file and be done with it?
public RunAction(String exeFileName){
this.runFileName = exeFileName;
}
publicvoid runFile(String exeFileName){
try{
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(runFileName);
InputStream stderr = proc.getErrorStream();
InputStreamReader isr =new InputStreamReader(stderr);
BufferedReader br =new BufferedReader(isr);
String line =null;
System.out.println("<ERROR>");
while ( (line = br.readLine()) !=null)
System.out.println(line);
System.out.println("</ERROR>");
int exitVal = proc.waitFor();
System.out.println("Process exitValue: " + exitVal);
}catch (Throwable t)
{
t.printStackTrace();
}
}
I apprecaite any insight offered.
Thx
[2015 byte] By [
xavier33a] at [2007-10-2 12:58:39]

That's the way the bat file was told to work. It is starting notepad, waiting for that process to end, and then carry on with the next command (if any) in the script.
This is not a Java-related issue. It is a bat file issue.
If you want to *spawn* Notepad (as a background process), then change the line in your script (bat file) to:
start NotePad
Hi Thx for the reply. That makes sense.
But how could I set it up to run the bat file independantly of the main program (that starts it)?
I just tried starting the notepad independently of a batch file and the program still hangs waiting for notepad to exit.
I also tried having 1 batch file start another bat file that started notepad. Thinking that once the 1st bat file starts the second it would end and my program would be able to continue on as desired. But this didn't work.
Also just to clarify. I'm just testing with notepad. Its not necessarily a typical end use of this class.
> I just tried starting the notepad independently of a
> batch file and the program still hangs waiting for
> notepad to exit.
You mean because of this line?
> int exitVal = proc.waitFor();
It's just doing what you told it to do - "wait for" it to terminate.
Don't do that part then, at least not at that point in the app.