File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Running Ant from Console App Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Running Ant from Console App" Watch "Running Ant from Console App" New topic

Running Ant from Console App

Danish Shaukat
Ranch Hand

Joined: Nov 16, 1999
Posts: 340
Hi !
I am trying to run Ant from my console application. I don't want to use
a batch file. I am having problems with my command and need some help.
My build script is in D:\apps\serverapps
And here is the command
String[] cmd = new String[4];
cmd[0] = "cmd.exe" ;
cmd[1] = "/c" ;
cmd[2] = "cd d:\\apps\\serverapps";
I am getting the following error message:
"The system cannot find the path specified".
If I don't specify the fourth array element or fourth command then I dont get any error. But on specifying the fourth command I get error, be it "ant" or "cd" or "dir".
[ February 25, 2004: Message edited by: Danish Shaukat ]
Joe Ess

Joined: Oct 29, 2001
Posts: 9168

The array passed to exec() should contain the command-line tokens of a single command, not a list of commands (i.e. "CD" and "ant"). You can't change the working directory using "CD" anyway, since it will be invoked in a seperate process. Check out the java.lang.Runtime documentation. There's a couple of exec() methods that take the working directory as an argument or you could probably get away with passing the full path of the build.xml file to ant if you used full (not local) paths in it.

[How To Ask Questions On JavaRanch]
Danish Shaukat
Ranch Hand

Joined: Nov 16, 1999
Posts: 340
Thanks Joe !
My problem is solved.
[ February 25, 2004: Message edited by: Danish Shaukat ]
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
By doing such a thing, you are breaking one of the design intentions of Ant - to remain platform independant.
Why aren't you calling on the Ant APIs to start a build ?
Those APIs were exposed for that exact reason.

Tony Morris
Java Q&A (FAQ, Trivia)
wood burning stoves
subject: Running Ant from Console App
It's not a secret anymore!