Calling ant from java

public static void main(String[] args) {

String arr[] = null;

arr = new String[2];

arr[0] = "-buildfile";

arr[1] = "C:\\workspace\\my projects\\ANTTASKS\\bin\\build.xml";

try {

Launcher launcher = new Launcher();

launcher.main(arr);

} catch (Exception e) {

System.out.println(e.getMessage());

// TODO: handle exception

}

},

I tried calling ant from java. But the problem is that, i am not sure how to get the handle of error & input stream from this.

I also need to know how do I kill the ant executable if there is some kind of exception like ConnectionTimeOut or is there any some kind of timeout mechanism.

Earlier i was calling ant as an external process like Process P = new Process(), where p.execute("ant.bat xxxxx"). I used to get the error & input stream from the process itself.

Please help!!!

thanks

[927 byte] By [Shashank.Tilwallia] at [2007-11-27 8:57:40]
# 1
http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html
aniseeda at 2007-7-12 21:22:33 > top of Java-index,Java Essentials,Java Programming...
# 2

A better approach would be to use the classes provided in Ant. The API documentation for Ant has all the necessary information.

The sample code would look like:

Project ant = new Project();

ProjectHelper helper = new ProjectHelperImpl();

ant.init();

helper.parse(ant, new File("build.xml"));

ant.executeTarget("clean");

aniseeda at 2007-7-12 21:22:33 > top of Java-index,Java Essentials,Java Programming...
# 3
thanks, i tried the code. It worked but it did not give any out. There were no error or output generated. I'll just check ..
Shashank.Tilwallia at 2007-7-12 21:22:33 > top of Java-index,Java Essentials,Java Programming...
# 4
hi,,how do we getting the error out put & the standard input stream when we call ant in the above manner. Also is ther way of killing this invocation in case the ant scripts hangs....
Shashank.Tilwallia at 2007-7-12 21:22:33 > top of Java-index,Java Essentials,Java Programming...
# 5

> hi,,

> how do we getting the error out put & the standard

> input stream when we call ant in the above manner.

> Also is ther way of killing this invocation in case

> the ant scripts hangs....

Don't use Runtime.exec(), use the other approach.

aniseeda at 2007-7-12 21:22:33 > top of Java-index,Java Essentials,Java Programming...
# 6
I am not using runtime.exec(), I would like to capture the error output.So I calling ant within my java program. But how do I stop the ant execution in case I want to. For eg. stopping the execution if there is an timeout exception.Secondly how do I get the error output.
Shashank.Tilwallia at 2007-7-12 21:22:33 > top of Java-index,Java Essentials,Java Programming...
# 7
if there's a timeout in Ant it will stop itself :)Just read the Ant documentation, it's there for a reason...
jwentinga at 2007-7-12 21:22:34 > top of Java-index,Java Essentials,Java Programming...