File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes executing batch file from a java program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "executing batch file from a java program" Watch "executing batch file from a java program" New topic
Author

executing batch file from a java program

Neeraj Gupta
Greenhorn

Joined: Jun 21, 2003
Posts: 10
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

Joined: Jan 06, 2005
Posts: 140
What about this ?


Hope that helps,


Lionel Badiou
CodeFutures Software
Lionel Badiou
Ranch Hand

Joined: Jan 06, 2005
Posts: 140
Horatio Westock
Ranch Hand

Joined: Feb 23, 2005
Posts: 221
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

Joined: Mar 22, 2000
Posts: 12809
    
    5
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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: executing batch file from a java program