Ant javac classpathref not working?

mauryat picture mauryat · Jan 18, 2013 · Viewed 8.6k times · Source

The javac task in my compile target fails despite the required jars being on the classpath. Could anyone help me figure out why this is happening?

The error below is caused because IOUtils.lineIterator() is not recognized. However, commons-io-1.3.1.jar is on the build path (in ${app.lib.dir}). So, it's strange that the build fails.

<target name="compile">
    <mkdir dir="${classes.dir}" />
    <javac destdir="${classes.dir}" debug="true" classpathref="dependency.path"
        excludes="/../security/**" >
        <src path="${java.src.dir}" />
    </javac>
</target>

<path id="dependency.path">
    <fileset dir="${spring.lib.dir}" includes="*.jar" />
    <fileset dir="${spring.depends.dir}" includes="**/*.jar" />
    <fileset dir="${app.lib.dir}" includes="*.jar" />
    <fileset dir="${tomcat.dir}/.." includes="**/*.jar" />
</path>

Execution

ant -f build-dev.xml compile

Buildfile: C:\..\build-dev.xml compile:
    [mkdir] Created dir: C:\..\classes
    [javac] Compiling 348 source files to C:\..\classes
    [javac] C:\..\UploadUtil.java:132: cannot find symbol
    [javac] symbol  : method lineIterator(java.io.InputStream,java.lang.String)
    [javac] location: class org.apache.commons.io.IOUtils
    [javac]             Iterator<String> it = IOUtils.lineIterator(is, charSet);
    [javac]                                          ^
    [javac] Note: Some input files use or override a deprecated API.
    [javac] Note: Recompile with -Xlint:deprecation for details.
    [javac] Note: Some input files use unchecked or unsafe operations.
    [javac] Note: Recompile with -Xlint:unchecked for details.
    [javac] 1 error

BUILD FAILED
C:\..\build-dev.xml:54: Compile failed; see the compiler error output for details.

Total time: 11 seconds

Approaches tried

  1. Eclipse, surprisingly, is building the project without any problem. I have the same set of jars on the project build path. I also have the same source directory excluded from the compilation.
  2. Attempted to use javac executable on the source folder, but not sure how to use javac on a source-code directory structure.
  3. I'm sure the version of the jar I have has the class IOUtils because Eclipse shows it in the contents of the jar.

Answer

Mark O&#39;Connor picture Mark O'Connor · Jan 18, 2013

The most likely explanation is that the jar containing the class is not on the classpath.

I double-checked Maven Central, just in case there was a jar called "commons-io-1.3.1" that does not contain this class, but sadly no:

So I'd suggest is that you add the following diagnostic target into your build so that you can confirm that the jar is in fact on the classpath:

<target name="diagnostics">
    <pathconvert property="classpathProp" refid="dependency.path"/>

    <echo>Classpath is ${classpathProp}</echo>
</target>