Help coderanch get a
new server
by contributing to the fundraiser
  • 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Calling Ant From Java

 
Ranch Hand
Posts: 393
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I need to call ant from java. Currently I am calling ant as a subprocess in my java code for ex
Process p = new Process()'
p.execute("ant.bat xxxx")

I need to call the ant within my java code. Can someone help me out. I tried viewing launcher.jar but I see that the run method in launcher class is private methods. IS THERE ANY OTHER way to call ant within java code.

Thanks
 
Nikhil Jain
Ranch Hand
Posts: 393
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I included ant.jar & ant-launcher.jar

I wrote the following code


Is this the correct way of calling ant from java. Secondly I get the following output when I execute the above code.



It executed fine but it also gave unable to locate tools.ajr error.
I called the code from eclipse.

Thanks
Shashank
 
Nikhil Jain
Ranch Hand
Posts: 393
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
again, the problem which I am facing is how do I get errors or kill the code if its taking lot of time eg. connection timout out...
 
Saloon Keeper
Posts: 28054
198
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://ant.apache.org/manual/running.html#viajava

Somewhere in the Ant docs I think there's something on invoking Ant from within another Java app, but you can also start a new JVM using the information above and the Runtime.exec() facility. Runtime will allow you to capture stdout and stderr.

For more sophisticated operation such as the wat Ant is invoked from IDEs there's also some callback functionality listed in the Ant docs somewhere. I think it was originally under "Ant in anger".
 
Nikhil Jain
Ranch Hand
Posts: 393
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I currently using runtime.exec() only. The problem with this is that, it acts like an external process & spawns another thread. I wanted to invoke ant as a normal java class. But when I do that, I don't get ant's error & input. Is there any way of doing this.

Thanks
Shashank
 
Author
Posts: 3473
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Ant launcher may not process environment variables since java
does not provide access to these. You can try one of the following

1. Just use the java command from the JDK area directly and not rely on
the operating System path to find it.

%JAVA_HOME%\bin\java -jar ant-launcher.jar -buildfile /path/to/build.xml

2. make sure tools.jar in the classpath

java -cp %JAVA_HOME%\lib\tools.jar;ant-launcher.jar
org.apache.tools.ant.launch.Launcher -buildfile /path/to/build.xml
 
Tim Holloway
Saloon Keeper
Posts: 28054
198
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Shashank Tilwalli:
Hi,

I currently using runtime.exec() only. The problem with this is that, it acts like an external process & spawns another thread. I wanted to invoke ant as a normal java class. But when I do that, I don't get ant's error & input. Is there any way of doing this.

Thanks
Shashank



OK, if you want to run Ant inside the same JVM as the spawning program, as I said, there should be some stuff in the Ant docs on how to do that - I'm just too lazy to track them down. Part of that documentation includes how to jack in callbacks to catch the output from Ant.
 
reply
    Bookmark Topic Watch Topic
  • New Topic