aspose file tools*
The moose likes Beginning Java and the fly likes Execute a .bat file? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Execute a .bat file?" Watch "Execute a .bat file?" New topic
Author

Execute a .bat file?

Michael Hildner
Ranch Hand

Joined: Oct 13, 2000
Posts: 297
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

Joined: Jun 26, 2000
Posts: 103
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

Joined: Oct 13, 2000
Posts: 297
Thanks, I tried a bunch of different ways, but never with 'start'. Works fine.
chakri san
Greenhorn

Joined: Jul 24, 2001
Posts: 2
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

Joined: Apr 23, 2004
Posts: 2
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

Joined: Dec 10, 2001
Posts: 7023
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.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Stevanus Setiawan
Greenhorn

Joined: Apr 23, 2004
Posts: 2
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

Joined: Jun 06, 2005
Posts: 1
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

Joined: Dec 29, 2004
Posts: 1071
.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

Joined: Jul 11, 2001
Posts: 14112
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.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Execute a .bat file?