• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Execute a .bat file?

 
Ranch Hand
Posts: 297
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, I tried a bunch of different ways, but never with 'start'. Works fine.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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


 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);
}
}
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
And then the entire population worshiped me like unto a god. Well, me and this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic