Apache ant - compile and run with build.xml

842 views Asked by At

I want to compile and then execute my program, giving it 2 input parameters. Why doesn't it work?

<?xml version="1.0" encoding="UTF-8"?>
<project default="run" name="MyProgram compile and run">

<target name="run" depends="compile"> 
    <classpath path="." />
    <exec executable="MyProgram">
        <arg value="80"/>
        <arg value="C:/"/>
    </exec> 
</target>

<target name="compile">
    <javac srcdir="." destdir="." />
</target>

</project>
1

There are 1 answers

0
David W. On BEST ANSWER

Exactly what are you trying to do? You're compiling a bunch of Java code, and then using exec to execute MyProgram. Is that file named MyProgram.exe? Or, is this Java code?

If MyProgram the java code you're compiling via the javac, you may need to use the <java> task which runs the Java Runtime Engine to execute the compiled *.class files.

I am going to assume you have a single Java file called MyProgram.java, and you want to compile it and execute MyProgram.class.

<project name="MyProgram" default="run" basedir=".">   <!-- No spaces in project name! -->

    <target name="clean">
        <delete dir="${basedir}/dest"/>
    </target>

    <!-- Put the Java Source and Destination under their own directories -->
    <target name="compile">
        <mkdir dir="${basedir}/dest"/>
        <javac srcdir="${basedir}/src"
            destdir="${basedir}/dest"/>
    </target>

    <target name="run">
        <!-- Assuming file called dest/MyProgram.class was built -->
        <java classname="MyProgram"
            classpath="${basedir}/dest">
            <arg value="80"/>
            <arg value="C:"/>
       </java>
    </target>