44

I have an Apache Ant build file with a <javac> command that requires four specific JARs to be on the build classpath. I've tried to do this:

<project basedir=".." default="build_to_jar" name="Transnet Spectrum Analyzer">
    <property environment="env"/>
    <property name="src" value="src"/>
    <property name="libsrc" value="library_sources" />
    <property name="build" value="build"/>
    <property name="dist" value="dist"/>
    <property name="target" value="1.5"/>
    <property name="libraries" value="./libraries"/>

    <fileset dir="." id="TSA.classpath">
        <include name="${libraries}/rxtx/RXTXcomm.jar" />
        <include name="${libraries}/commons-logging-1.1.1.jar" />
        <include name="${libsrc}/JCommon/jcommon-1.0.15.jar" />
        <include name="${libsrc}/JFreeChart/jfreechart-1.0.12.jar" />
    </fileset>

    <target name="compile" depends="clean,init" description="compile the source " >

        <echo>Compile from ${src} to ${build}</echo>

        <javac destdir="${build}" source="${target}" target="${target}">
            <compilerarg value="-Xlint:unchecked"/>
            <src path="${src}"/>
            <classpath path="TSA.classpath" />
        </javac>
    </target>

    <!-- blah blah blah -->
</project>

…but none of the files specified in TSA.classpath appear to show up. How do I include these files in my classpath?

Paul Fisher
  • 9,618
  • 5
  • 37
  • 53

3 Answers3

74

Here's an example from a project I am currently working on. I suspect you can modify it to work for your situation.

<path id="master-classpath">
  <fileset dir="${web.dir}/WEB-INF/lib">
    <include name="*.jar"/>
  </fileset>

  <fileset dir="${appserver.lib}">
    <include name="servlet*.jar"/>
  </fileset>

  <pathelement path="${build.dir}"/>
</path>

...

<javac destdir="${build.dir}">
  <src path="${src.dir}"/>
  <classpath refid="master-classpath"/>
</javac>
William Brendel
  • 31,712
  • 14
  • 72
  • 77
  • I'm defining the ' ... ' from an XML file and then using '' to call another XML file which knows how to build build my classes. If the path id is defined in the 'external' XML it works fine, but if defined in the calling XML I get an error. Do you know if these can be in separate XML's and if so, why I'm getting this error? – Jeach May 29 '12 at 03:57
15

Try

<javac ... classpathref="TSA.classpath">

or

<javac ...>
    ...
    <classpath refid="TSA.classpath" />
</javac>
Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
  • It's worth reading this after the one that was chosen as Best Answer (by @William Brendel). Thanks :) –  Sep 14 '12 at 15:21
  • It's still rather sad there are at least three different ways to do this in Ant, but the more the merrier, right? (A third would be to include a fully qualified classpath within javac, a fourth would be to make a partial classpath that partially references another one... etc.) –  Sep 14 '12 at 18:17
  • Just to clarify, the second one doesn't work, at least where `TSA.classpath` is a ``. – StockB Apr 29 '13 at 20:26
14

Try this:

 <classpath refid="TSA.classpath"/>
TofuBeer
  • 60,850
  • 18
  • 118
  • 163