简体   繁体   中英

Ant:Executing a java program from ant and storing the returned value

I am a new to Ant. I am basically trying to execute a java program from ant. I have managed to get that working. However, I am not sure how to capture the value returned by the program in ant in order to use it within the ant file for further operations.

build.xml

<?xml version="1.0" encoding="UTF-8"?>
<project default="run" name="Experiment 1">
    <property name="shimura" value="Danzo!!!"/>
    <target name="run" depends="compile">
        <java classname="com.sim.expt.Demo">
            <classpath path="./staging" />
            <arg line="${shimura}"/>
        </java>
    </target>
    <target name="compile">
        <javac srcdir="./src" includeantruntime="false" destdir="./staging" />
    </target>
</project>

Java Program:

package com.sim.expt;

public class Demo {

    public static void main(String[] args) {
        String name=args[0];
        System.out.println("Nani!!!!!!!"+name);
    }

}

Use the outputproperty attribute, as detailed at https://ant.apache.org/manual/Tasks/java.html

<?xml version="1.0" encoding="UTF-8"?>
<project default="run" name="Experiment 1">
    <property name="shimura" value="Danzo!!!"/>
    <target name="run" depends="compile">
        <java classname="com.sim.expt.Demo" outputproperty="myprop">
            <classpath path="./staging" />
            <arg line="${shimura}"/>
        </java>
    </target>
    <target name="compile">
        <javac srcdir="./src" includeantruntime="false" destdir="./staging" />
    </target>
</project>

Then ${myprop} will contain the output from stdout and stderr (unless separately redirected).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM