This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Building Blockchain Apps and have Michael Yuan on-line!
See this thread for details.
Win a copy of Building Blockchain Apps this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

How to call BAT file from JAVA programs ?

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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.
 
Why am I so drawn to cherry pie? I can't seem to stop. Save me tiny ad!
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!