tags:

views:

261

answers:

2

Completely new to Ant and I have just a simple problem. I'm running ant with a input file: ant -Dargs="input.txt" run but it says it cannot find input.txt (No such file or directory). I have the build.xml file and input.txt in the same directory, above src and bin. Here is my build.xml file, am I missing something?

<project name="Project" default="compile" basedir=".">

  <description>
    A build file for a project
  </description>

  <!-- global properties for this build file -->
  <property name="source.dir" location="src"/>
  <property name="build.dir" location="bin"/>
  <property name="doc.dir" location="doc"/>
  <property name="main.class" value="proj.ProjMain"/>

  <!-- set up some directories used by this project -->
  <target name="init" description="setup project directories">
    <mkdir dir="${build.dir}"/>
    <mkdir dir="${doc.dir}"/>
  </target>

  <!-- Compile the java code in ${src.dir} into ${build.dir} -->
  <target name="compile" depends="init" description="compile java sources">
    <javac srcdir="${source.dir}" destdir="${build.dir}"/>
  </target>

  <!-- execute the program with the fully qualified name in ${build.dir} -->
  <target name="run" description="run the project">
    <java dir="${build.dir}" classname="${main.class}" fork="yes">
        <arg line="${args}"/>
    </java>
  </target>

  <!-- Delete the build & doc directories and Emacs backup (*~) files -->
  <target name="clean" description="tidy up the workspace">
    <delete dir="${build.dir}"/>
    <delete dir="${doc.dir}"/>
    <delete>
      <fileset defaultexcludes="no" dir="${source.dir}" includes="**/*~"/>
    </delete>
  </target>

  <!-- Generate javadocs for current project into ${doc.dir} -->
  <target name="doc" depends="init" description="generate documentation">
    <javadoc sourcepath="${source.dir}" destdir="${doc.dir}"/>
  </target>

</project>
+4  A: 

The command ant -Dargs="input.txt" run will run the following target:

  <!-- execute the program with the fully qualified name in ${build.dir} -->
  <target name="run" description="run the project">
    <java dir="${build.dir}" classname="${main.class}" fork="yes">
        <arg line="${args}"/>
    </java>
  </target>

As written in the comment, this target executes a Java program from the ${build.dir} directory (which is here bin), passing the command line arguments to it. So, if input.txt is above bin, you should run:

$ ant -Dargs="../input.txt" run
Pascal Thivent
Ah ok great thanks so much!
A: 

The basedir parameter in the project tag indicates the directory from which the build file's reference are taken when the absolute path is not specified. So your input.txt should be relative to your basedir or you should mention the absolute path to your input.txt.

In your case, the basedir refers to the directory where your build file resides. So, if the input.txt is in the same directory as your build file is, it should work fine

Ram
It's `proj.ProjMain` that is complaining, not ant.
Pascal Thivent