473,581 Members | 6,733 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Running Ant using Java..

38 New Member

I have written a java code to execute ant targets via Servlet. I have given the code below.

Expand|Select|Wrap|Line Numbers
  1. import org.apache.tools.ant.*;
  2. import org.apache.catalina.ant.*;
  3. .
  4. .
  5. .
  6. .
  7. public void antTest()
  8. {
  9.     BuildLogger logger = new DefaultLogger();
  10.     logger.setMessageOutputLevel(Project.MSG_INFO);
  11.     logger.setOutputPrintStream(System.out);
  12.     logger.setErrorPrintStream(System.out);
  13.     logger.setEmacsMode(true);
  15.     ProjectHelper ph = ProjectHelper.getProjectHelper();
  16.     Project p = new Project();
  17.     p.addBuildListener(logger);
  18.     p.init();
  19.     p.addReference("ant.projectHelper", ph);
  20.     ph.parse(p, new File(sctx.getRealPath("/")+"bin/build.xml"));
  21.     p.executeTarget("display");
  22. }
The build.xml file is as follows.

Expand|Select|Wrap|Line Numbers
  1. <project name="Web Application" default="display" basedir=".">
  2.     <property file="${user.home}/build.properties"/>
  4.     <property file="build.properties"/>
  6.     <target name="init">
  7.         <tstamp/>
  8.     </target>
  10.     <target name="display" description="Deletes the Web Application's war directory and war file">
  11.         <echo message="This line is displaying from ant target ..."/>
  12.     </target>
  14. </project>
When the method antTest() is invoked, I got the following output.

Expand|Select|Wrap|Line Numbers
  1. This line is displaying from ant target ...
When I executed the build.xml using the command prompt (i.e. using the command "ant -f build.xml display"), I got the following output.

Expand|Select|Wrap|Line Numbers
  1. Buildfile: build.xml
  2. Trying to override old definition of datatype resources
  4. display:
  5.      [echo] This line is displaying from ant target ...
  8. Total time: 0 seconds
I would like to have the following.

1) I expect the output from the java code should also display the same lines as in the output from command prompt. Please let me know the way to do so.

2) I want to log the output of the ant target to a log file when executing from java.

3) Is there any other way to run ant targets as we are running from the command prompt. That is if we have a string variable having the value "ant -f build.xml display", we could have to make this as the command to execute ant target. Is this possible? If so please let me know the way to implement.
Dec 27 '08 #1
6 14156
38 New Member
Is anyone having solution for this??
Jan 1 '09 #2
785 Recognized Expert Contributor
If you want the exact output from Ant as if running it from command line, then why don't you run it from command line through java then?

Here I wrote a function where you need to pass the command and working directory only and you get back the output from this command as a string result. Then you can save this result to a log file.

In your case, call it with
Expand|Select|Wrap|Line Numbers
  2. executeShell("ant -f build.xml display", "c:\\myAntInstallationDirectory\\bin")
Expand|Select|Wrap|Line Numbers
  2.  /////////////////////////////////////////////////////////////////////////////////////////// 
  3.  public static final String executeShell(String command, String dir)
  4.  throws Exception
  5.  {
  6.   Runtime runtime = Runtime.getRuntime();
  7.   String output="";
  8.   try 
  9.   {
  10.    File fileDir = new File(dir);
  11.    Process process = runtime.exec(command, null, fileDir);
  12.    BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
  14.    // Read the output
  15.    String line = null;
  16.    while ((line = in.readLine()) != null)
  17.    {
  18.     output += line + "\n";
  19.    }
  20.   }
  21.   catch (Exception exception)
  22.   {
  23.    throw new Exception("[EXCEPTION in RuntimeUtils.executeShell()] : output of shell-command '" + command + "' = '" + output + "'. Exception=" + exception);
  24.   }
  26.   return output;
  27.  }
Jan 9 '09 #3
1 New Member
Hi chaarmann,

I've tried your solution but instead I got this error:

[EXCEPTION in RuntimeUtils.ex ecuteShell()] : output of shell-command 'ant -f build.xml display' = ''. Exception=java. io.IOException: Cannot run program "ant" (in directory "X:\build.xml") : CreateProcess error=2, The system cannot find the file specified

The file is there and it can be run through command line. Any idea?

Jun 1 '10 #4
3 New Member
Try below

cmd = new String[] { "sh", "-c", "PATH=/ant binaries path","ant -buildfile","You r ant build file args..."}

p = r4.exec(cmd);
Sep 4 '12 #5
3 New Member
for logs use the Record option to document the Target out put
Sep 4 '12 #6
3 New Member
<target name="ftp_recor d" >
<echo message="Starte d Recording .."/>

<record name="C:\App\lo gs\${ftp.user}_ ftp_log.txt" action="start" append="no"/>
Sep 4 '12 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

by: Jagdeesh | last post by:
Hai Colleagues, I am using Tomcat 4.1.24 and JDK 1.4.0_03 in my winXP machine. I've transferred a set of folders(containing jsp files) into tomcat's webapps directory(to /webapps/bob , /webapps/sue) and i have added the folders bob, sue in my server.xml(in the context path). When i am trying to run jsp files from my browser, it works...
by: Laura P | last post by:
Hi, I wasn't sure whether this should be posted in the Java are or in a Solaris thread, so I shall post it in both. Sorry for the duplication. I am new to Solaris and am having trouble running a long Java command from a shell script. Firstly, my Solaris (8) / Java setup. Mu machine already had Java 1.2
by: Peter Bassett | last post by:
I haven't programmed in Java for awhile and forgot how to get this to work. I have a program ListFiles developed in c:\java which lists every .JPG and ..GIF file in a folder. It works fine running from that folder. It takes 1 argument, the pathname, so a command could be "java ListFiles ." But if I try to run it from another folder, using...
by: Baddn Raj | last post by:
Hi, I have a newbie doubt with respect to DOM-Processing. I am using the Xerces2 Java Parser 2.6.2 , with the TrAX API for basically running some XSLT Transformations using the "transfomer" object ( From a DOMSource to a DOMResult). I have both "xercesImpl.jar" and "xalan.jar" in my classpath. The following program works fine from the...
by: nishi57 | last post by:
I hope I can get some help regarding this issue, which has been going on for a while. I have a desktop user who is having problem running "Stored Procedures". The DB2 Connect application works fine but when he runs the stored procedure, he gets the following error message. "SYSPROC".CSGCSB54 - Run started. Data returned in result sets is...
by: shanmukhi | last post by:
Hi All I got a problem in running java programs. i am not able to run java applet while running i got a problem as Loading Java Applet Failed java.lang.NoClassDefFoundError: App (wrong name: src/App) at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClass(Unknown Source)
by: rohitkec | last post by:
I am new to both java andb apache tomcat.i am presently using apache tomcat 4.1.31 & j2re-1_4_2_10.when i am running or admin file i am getting the following errors: org.apache.jasper.JasperException: Unable to compile class for JSP An error occurred at line: -1 in the jsp file: null Generated...
by: lifeisgreat20009 | last post by:
This is what i am getting on running the program......... C:\PROGRA~1\Java\JDK15~1.0\bin>javac Editor.java Note: Editor.java uses or overrides a deprecated API. Note: Recompile with -Xlint:deprecation for details. I did run the program like this after compiling it using javac command......... I get this........
by: shaileshkumar | last post by:
hi, iam unable to run my applications in netbeans4.1. problem may be due to JVM or port numbers conflict. i did not intentionally change any port numbers . please guide me accordingly. iam pasting the error report ( thanks in advance) -------------------------------------------------------------------------
by: WP | last post by:
Hello, I have a very simple script (or would you call it a batch file?) with the following content: connect to mydb2; DROP TABLE staff_employee_address; DROP TABLE staff_employee_address_telephone; DROP TABLE staff_employee; DROP TABLE staff; commit; terminate;
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.