• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

How to call BAT file from JAVA programs ?

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
Can anybody inform me, how to call a bat file from java program...using..
Runtime.getRuntime().exec()..
I tried in the follwoing way but its not working.. but when I am calling an exe file then its working..
Process p = runtime.exec("cmd /c c:\\sunTest\\test.bat"); // not working..
Process p = runtime.exec("cmd /c calc"); //working..
Thanks in advance.
Regards,
Sundeep.
 
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The output generated by the .bat file is being considered as the name of an executable file unless you specifically use ECHO command.
Here is the code I have tried and it's working fine.
a.bat
*****
ECHO INDIA
--------------------------
Runtime runtime = Runtime.getRuntime();
try
{
Process p1 = runtime.exec("cmd /c D:\\temp\\a.bat");
InputStream is = p1.getInputStream();
int i = 0;
while( (i = is.read() ) != -1)
{
System.out.print((char)i);
}

}
catch(IOException ioException)
{
System.out.println(ioException.getMessage() );
}
-----------------------------------------------------
OutPut
******
D:\eclipse\workspace\Temp>ECHO INDIA
INDIA
 
Sundeep Mohanty
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by VIJAY Yadlapati:
The output generated by the .bat file is being considered as the name of an executable file unless you specifically use ECHO command.
Here is the code I have tried and it's working fine.
a.bat
*****
ECHO INDIA
--------------------------
Runtime runtime = Runtime.getRuntime();
try
{
Process p1 = runtime.exec("cmd /c D:\\temp\\a.bat");
InputStream is = p1.getInputStream();
int i = 0;
while( (i = is.read() ) != -1)
{
System.out.print((char)i);
}

}
catch(IOException ioException)
{
System.out.println(ioException.getMessage() );
}
-----------------------------------------------------
OutPut
******
D:\eclipse\workspace\Temp>ECHO INDIA
INDIA


Hi Vijay,
Thank you for ur reply. Actually I tried on this manner but it gave me some error(not exacly error but the process is not teminating).
So, I changed my command to the following ways and now its working...
Process p = runtime.exec("cmd /c start c:\\sunTest\\test.bat");
Thank you once again for ur answer.
Regards,
Sundeep.
reply
    Bookmark Topic Watch Topic
  • New Topic