Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Execute a .bat file?

 
Michael Hildner
Ranch Hand
Posts: 297
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it possible to use Runtime.getRuntime().exec... to execute a batch file on win32? Works for executables, can't seem to launch a batch file.
 
Savithri Devaraj
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Michael Hildner:
Is it possible to use Runtime.getRuntime().exec... to execute a batch file on win32? Works for executables, can't seem to launch a batch file.

You may try this --
Runtime.getRuntime().exec("cmd /K start run.bat");
This worked for me. You may use /K or /C options. Replace run.bat with your batch filename.
A batch file is interpreted by the command interpreter.
Savithri
 
Michael Hildner
Ranch Hand
Posts: 297
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, I tried a bunch of different ways, but never with 'start'. Works fine.
 
chakri san
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
thank you i sloved my problem
cheers,
chakri
Originally posted by Savithri Devaraj:
You may try this --
Runtime.getRuntime().exec("cmd /K start run.bat");
This worked for me. You may use /K or /C options. Replace run.bat with your batch filename.
A batch file is interpreted by the command interpreter.
Savithri

 
Stevanus Setiawan
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hai all,
But when u programming under windows application it doesn't work.
for example:
private void button1_click(Object source, Event e)
{
Runtime.getRuntime().exec("cmd /c somedirectory/dir/files.bat");

}
are anybody know to solve this problem ?
thx,
Stev
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch, Stevanus!
Look more closely at the suggested solution. You left out the "start".
Note that this only works on some version of the Windows operating system, and it certainly won't work on Linux.
 
Stevanus Setiawan
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thnx ,
it worked i used this syntax:
private void button2_click(Object source, Event e)
{
try {
Runtime.getRuntime().exec("E:/directoray/ant/file.bat");
} catch (IOException eOut) {
System.exit(0);
}
}
 
Dantaress lim
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how do u execute a bat file without a start --> Runtime.getRuntime().exec("cmd /K start run.bat"); ?

coding with the start,it will keep opening the command prompt.so how do u solve tis.
 
Steven Bell
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
.bat files have to run in a command promt in windows. You can't get around that unless you skip the .bat file, do whatever the .bat file is doing in C/C++, and use JNI to get to it.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Dirk Schreckmann:
Note that this only works on some version of the Windows operating system


On older versions, it was "command" instead of "cmd", if I remember correctly. You can find out by examining the "ComSpec" environment variable.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic