views:

70

answers:

1

I've dealt with errors like this in Eclipse before, but I have no idea why I'm getting it this time. I have the Apache Commons IO library in my Build Path as well as in my "lib" folder.

I've given the error below. It's pretty straightforward.

        [javac] Compiling 3 source files to C:\Users\Justian\workspaces\ConnectionCompiler\build
        [javac] C:\Users\Justian\workspaces\ConnectionCompiler\src\jab\jm\readers\ExcelReader.java:5: package org.apache.commons.io does not exist
        [javac] import org.apache.commons.io.FileUtils;
        [javac]                             ^
        [javac] C:\Users\Justian\workspaces\ConnectionCompiler\src\jab\jm\readers\FileManager.java:5: package org.apache.commons.io does not exist
        [javac] import org.apache.commons.io.FileUtils;
        [javac]                             ^
        [javac] C:\Users\Justian\workspaces\ConnectionCompiler\src\jab\jm\readers\FileManager.java:12: cannot find symbol
        [javac] symbol  : variable FileUtils
        [javac] location: class jab.jm.readers.FileManager
        [javac]         return FileUtils.convertFileCollectionToFileArray(FileUtils.listFiles(
        [javac]                                                           ^
        [javac] C:\Users\Justian\workspaces\ConnectionCompiler\src\jab\jm\readers\FileManager.java:12: cannot find symbol
        [javac] symbol  : variable FileUtils
        [javac] location: class jab.jm.readers.FileManager
        [javac]         return FileUtils.convertFileCollectionToFileArray(FileUtils.listFiles(
        [javac]                ^
        [javac] 4 errors

Why can't it import the class? It's even suggested that I add that specific one with Eclipse's auto-correct.

Many thanks!

Justian

EDIT:

Oh. Sorry. Been working on multiple things at once. Of course this would be an Ant issue.

Ok. Here's my build file. What's interesting is that this has worked in the past. Why would it not work now?

<?xml version="1.0" ?>

<project name="ServerJar" default="dist" basedir=".">
    <description>
        Builds client files into .jar
    </description>
    <!-- [build variables] -->
    <property name="src" location="src" />
    <property name="build" location="build" />
    <property name="dist" location="dist" />

    <target name="init">
        <!-- makes time stamp to be used in jar name -->
        <tstamp />
        <!-- creates build directory structure -->
        <mkdir dir="${build}" />
    </target>

    <target name="compile" depends="init" description="Compiles the source">
        <!-- compiles the java code from ${src} into ${build} -->
        <javac srcdir="${src}" destdir="${build}" />
    </target>

    <target name="dist" depends="compile" description="Generates distributable">
        <!-- creates the distribution directory -->
        <mkdir dir="${dist}/lib" />

        <!-- puts everything in ${build} into the jar file -->
        <jar jarfile="${dist}/lib/CC-${DSTAMP}.jar" basedir="${build}">
            <manifest>
                <attribute name="Main-Class" value="jab.jm.Test" />
            </manifest>
        </jar>

        <!-- makes a jar file for quick test execution -->
        <jar jarfile="${dist}/lib/CC.jar" basedir="${build}">
            <manifest>
                <attribute name="Main-Class" value="jab.jm.Test" />
            </manifest>
        </jar>
    </target>

    <target name="clean" description="Cleans up the extra build files">
        <!-- deletes the ${build} and ${dist} directories -->
        <delete dir="${build}" />
        <delete dir="${dist}" />
    </target>
</project>

EDIT: For anyone who has this problem in the future, here was my final build file:

<?xml version="1.0" ?>

<project name="ServerJar" default="dist" basedir=".">
    <description>
        Builds client files into .jar
    </description>
    <!-- [build variables] -->
    <property name="src" location="src" />
    <property name="build" location="build" />
    <property name="dist" location="dist" />
    <property name="lib" location="lib" />
    <!-- [path to packages] -->
    <path id="master-classpath">
        <fileset dir="${lib}">
            <include name="*.jar"/>
        </fileset>
    </path>


    <target name="init">
        <!-- makes time stamp to be used in jar name -->
        <tstamp />
        <!-- creates build directory structure -->
        <mkdir dir="${build}" />
    </target>

    <target name="compile" depends="init" description="Compiles the source">
        <!-- compiles the java code from ${src} into ${build} -->
        <!-- <javac srcdir="${src}" destdir="${build}" /> -->
        <javac destdir= "${build}">
            <src path="${src}"/>
            <classpath refid="master-classpath"/>
        </javac>
    </target>

    <target name="dist" depends="compile" description="Generates distributable">
        <!-- creates the distribution directory -->
        <mkdir dir="${dist}/lib" />

        <!-- puts everything in ${build} into the jar file -->
        <jar jarfile="${dist}/lib/CC-${DSTAMP}.jar" basedir="${build}">
            <manifest>
                <attribute name="Main-Class" value="jab.jm.Test" />
            </manifest>
        </jar>

        <!-- makes a jar file for quick test execution -->
        <jar jarfile="${dist}/lib/CC.jar" basedir="${build}">
            <manifest>
                <attribute name="Main-Class" value="jab.jm.Test" />
            </manifest>
        </jar>
    </target>

    <target name="clean" description="Cleans up the extra build files">
        <!-- deletes the ${build} and ${dist} directories -->
        <delete dir="${build}" />
        <delete dir="${dist}" />
    </target>
</project>
+5  A: 

You posted output from Ant.

Unless you are somehow integrating Eclipse with your build.xml file, Eclipse's idea of the classpath for your project is completely separate and mutually exclusive from the classpath used to build your project in your build.xml.

Solution: make sure your build.xml refers to the commons-io library when building your classes.


Update: From the build.xml snippet you've posted, looks like you are trying to compile your classes with no classpath references whatsoever. You need to tell the javac task where to find the library references.

Here is an example of using the javac task which refers to a classpath declared elsewhere:

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

<javac destdir="${classes.build.dir}">
    <src path="${src.dir}"/>
    <classpath refid="master-classpath"/>
</javac>

This sets up an Ant "path" which refers to every file ending in .jar in the directory pointed to by the ${lib.dir} property. You can of course change this to suit your needs, if for example you only want to refer to certain named jar files or you have several different directories containing your libraries.

matt b
Thanks for the followup, Matt. Only question is where to implement the second part of the code you provided into my build file? I'm not that great with Ant. The file I sent you was made over a year ago.Thanks!
Justian Meyer
The `<path>` part just needs to be declared anywhere in the file (I don't think Ant requires it to be declared before it is referenced), and my `javac` example should replace/supplement what you already have.
matt b