Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

executing batch file from a java program

 
Neeraj Gupta
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Can anyone please help me with code that how to execute a Windows based batch file through a simple java program.

I tried Runtime.exec() but its not working.

Assume that the batch file is kept at C:\ABC.bat

Neeraj
 
Lionel Badiou
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about this ?


Hope that helps,
 
Lionel Badiou
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Horatio Westock
Ranch Hand
Posts: 221
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think for a batch file, you have to pass it as a parameter to cmd.exe or command.com depending on your version of windows:

e.g. rt.exec("cmd.exe /C C:/test.bat");

Or use an array for the parameter with the arguments, your choice.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13064
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And remember that you have to provide for handling the output stream (std out) from the batch or it may never complete. This has been discussed here frequently, so browse the archives.
Bill
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic