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]
# 1

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

warnerjaa at 2007-7-13 10:16:54 > top of Java-index,Java Essentials,Java Programming...
# 2

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.

xavier33a at 2007-7-13 10:16:54 > top of Java-index,Java Essentials,Java Programming...
# 3
It seems liek the waitfor() is what the problem is... But I can't figure out another way of doing it without getting IllegalThreadStateException.
xavier33a at 2007-7-13 10:16:54 > top of Java-index,Java Essentials,Java Programming...
# 4

> 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.

warnerjaa at 2007-7-13 10:16:54 > top of Java-index,Java Essentials,Java Programming...
# 5
marnerja thx for your help!
xavier33a at 2007-7-13 10:16:54 > top of Java-index,Java Essentials,Java Programming...
# 6
Okay well, I thought that it was another issue but now I'm conviced that ti has to do with this code. I have removed the : int exitVal = proc.waitFor();But the program still hangs until the opened program is closed....Any ideas?
xavier33a at 2007-7-13 10:16:54 > top of Java-index,Java Essentials,Java Programming...
# 7
Consume the stdout and stderr output of the started process in separate threads so that I/O does not block your main thread.
BIJ001a at 2007-7-13 10:16:54 > top of Java-index,Java Essentials,Java Programming...