run a .bat file in the jar file

Hi,

In my java class I can open a notepad like this :

Runtime rt=Runtime.getRuntime();

Process process=rt.exec("C:\\WINDOWS\\NOTEPAD.EXE");

and it works, but if I want to run another bat fie, It doesn't show any activity, and does not work.

Process process=rt.exec("C:\\test.bat");

Does anyone know how to run a .bat file in the java or jar file?

Thanks.

[405 byte] By [APTa] at [2007-11-27 6:59:02]
# 1

> Hi,

> In my java class I can open a notepad like this :

> Runtime rt=Runtime.getRuntime();

> Process process=rt.exec("C:\\WINDOWS\\NOTEPAD.EXE");

>

> and it works, but if I want to run another bat fie,

> It doesn't show any activity, and does not work.

> Process process=rt.exec("C:\\test.bat");

>

> Does anyone know how to run a .bat file in the java

> or jar file?

> Thanks.

It's working, you're just not seeing it. If you're running it from windows, the "cmd" console will load, execute the bat and then close immediately thereafter. If you would like for the window to stay open, append a "pause" command to the end of the .bat file, or run the jar from the command prompt:

java -jar MyJar.jar

Navy_Codera at 2007-7-12 18:49:35 > top of Java-index,Java Essentials,New To Java...
# 2
I am running the jar file with the same command line in the cmd, but still is not working, I try to append a pause at the end of the batch file as you said, and let you know the result.
APTa at 2007-7-12 18:49:35 > top of Java-index,Java Essentials,New To Java...
# 3

> I am running the jar file with the same command line

> in the cmd, but still is not working, I try to

> append a pause at the end of the batch file as you

> said, and let you know the result.

Could you post the source of the .bat file and the line in your class where you're calling the .bat file?

Navy_Codera at 2007-7-12 18:49:35 > top of Java-index,Java Essentials,New To Java...
# 4
I changed the code to this, and it is running properly now. I think I had to add "start" to it.Thanks for your help and quick reply.Process process=rt.exec("cmd /c start c:\\test.bat");
APTa at 2007-7-12 18:49:35 > top of Java-index,Java Essentials,New To Java...
# 5
Good deal. Glad you got it working.
Navy_Codera at 2007-7-12 18:49:35 > top of Java-index,Java Essentials,New To Java...
# 6
Cool, now just make sure you have a pair of threads reading your STDERR and STDOUT. If you don't they could block. I think in JDK5 you can set a flag and only have to read STDOUT.
awyorka at 2007-7-12 18:49:35 > top of Java-index,Java Essentials,New To Java...