using ANT to automatic run JUNIT cases, setting up dependencies - java

I have the following setup for my application:
The project TestAll currently contain a java file that run all the TestAll javafiles in all my other projects and this works as expected. The problem im facing is that I want this TestAll.java to be run from a ant script and have it record the result in a report file. This javafile is dependent on all the other projects in my application.
This is what I have so far:
<?xml version="1.0"?>
<project name="Ant-Test" default="main" basedir=".">
<!-- Sets variables which can later be used. -->
<property name="src.dir" location="" />
<property name="build.dir" location="../bin" />
<property name="dist.dir" location="../dist" />
<property name="lib.dir" location="../lib" />
<property name="test.dir" location="../src" />
<property name="test.report.dir" location="../testreport" />
<target name="clean">
<delete dir="${build.dir}" />
<delete dir="${dist.dir}" />
</target>
<!-- Define the classpath which includes the junit.jar and the classes after compiling-->
<path id="junit.class.path">
<pathelement location="${lib.dir}/junit.jar" />
<pathelement location="${build.dir}" />
</path>
<target name="makedir">
<mkdir dir="${build.dir}" />
<mkdir dir="${dist.dir}" />
<mkdir dir="${test.report.dir}" />
</target>
<!-- Compiles the java code (including the usage of library for JUnit -->
<target name="compile" depends="clean, makedir">
<javac srcdir="${src.dir}" destdir="${build.dir}">
<classpath refid="junit.class.path" />
</javac>
</target>
<target name="junit" depends="compile">
<junit printsummary="on" fork="true" haltonfailure="yes">
<classpath refid="junit.class.path" />
<formatter type="xml" />
<batchtest todir="${test.report.dir}">
<fileset dir="${src.dir}">
<include name="**/*Test*.java" />
</fileset>
</batchtest>
</junit>
</target>
<!--Creates the deployable jar file -->
<target name="jar" depends="compile">
<jar destfile="${dist.dir}\se.testall.jar" basedir="${build.dir}">
<manifest>
<attribute name="Main-Class" value="test.Main" />
</manifest>
</jar>
</target>
<target name="main" depends="compile, jar, junit">
<description>Main target</description>
</target>
</project>
And the errors im getting is:
[javac] Compiling 1 source file to C:\Repositories\MyProject\TestAllProjects\bin
[javac] C:\Repositories\MyProject\TestAllProjects\src\se\testall\src\TestAllClass.java:6: error: package se.tlv.AProject.testall does not exist
[javac] import se.tlv.AnotherProject.testall.*;
[javac] ^
[javac] C:\Repositories\TestAllProjects\src\se\src\TestAllClass.java:7: error: package se.AnotherProject.testall does not exist
..and so on for all the internal imports in my TestAll project
This is most likley a classpath error where ANT in unable to find the files it needs, but I have no idea how to resolve it and have been trying for almost a full day. Any help is appreciated

The classpath provided to the javac task is: the junit jar, the build directory and the current directory.
Unless the current directory (where build.xml is located) is se, the javac task won't be able to find any java files to compile them.
Given that, the classpath for the javac task will need to include a path to each se directory in each project.
Edit:
Note: Unless you're planning on packaging the tests with the rest of the code, you should have two javac tasks that build to a build directory and a test build directory, then provide a path to each of those to junit so it can run the tests, but provide only the build directory path to the jar task.

Related

Error: Could not find or load main class OCI_HelloWorld

I am to new java, ant and to stackoverflow.
I have a build.xml file under Project_Dir director in windows and run "ant run" command to run OCI_HelloWorld.java file which is under "src" folder.
I am getting below error after running "ant run" command.
C:\Project_Dir>ant run
Buildfile: C:\Project_Dir\build.xml
run:
[java] Error: Could not find or load main class OCI_HelloWorld
[java] Java Result: 1
BUILD SUCCESSFUL
Total time: 0 seconds
Below is my build.xml file
<!-- define the classpath for building the project -->
<path id="build.classpath">
<!-- include jar files under lib directory -->
<fileset dir="lib" includes="*.jar" />
</path>
<!-- define the classpath for running the project -->
<path id="run.classpath">
<!-- include jar files under lib directory -->
<fileset dir="lib" includes="*.jar" />
<!-- include the jar file resulting from building the project -->
<pathelement path="build/dist/OCI_HelloWorld.jar" />
</path>
<!-- build the jar file for the project -->
<target name="build" description="Build the HelloWorld program">
<!-- first compile your java class code -->
<mkdir dir="build/classes" />
<javac includeantruntime="false" srcdir="src" destdir="build/classes" debug="true" includes="C:\Project_Dir\src\*.java" fork="yes" executable="C:\Program Files\Java\jdk1.7.0_79\bin\javac">
<classpath refid="build.classpath" />
</javac>
<!-- now assemble a jar file from the compiled classes -->
<mkdir dir="build/dist" />
<jar jarfile="build/dist/OCI_HelloWorld.jar" fork="yes" basedir="build/classes">
<fileset dir="build/classes" includes="**/*.*" />
</jar>
</target>
<!-- run the project jar file -->
<target name="run" description="Run the HelloWorld program">
<!-- run java on the resulting code -->
<java classname="OCI_HelloWorld" fork="true" >
<classpath refid="run.classpath" />
</java>
</target>
<!-- reset build environment to starting point -->
<target name="clean" description="Cleans up your project build artifacts">
<delete dir="build" />
</target>
Please let me know what I need to do to get rid of this error.

How to include local dependencies in my ant build

At present I have the following build.xml:
<project name="Bccn" default="help" basedir=".">
<!-- Define the properties used by the build -->
<property name="app.name" value="bccn" />
<property name="app.version" value="0.1-dev" />
<property name="tcserver.home" value="/home/abhishek/tomcat" />
<property name="work.home" value="${basedir}/work" />
<property name="dist.home" value="${basedir}/dist" />
<property name="src.home" value="${basedir}/src" />
<property name="web.home" value="${basedir}/web" />
<property name="lib.dir" value="${basedir}/lib" />
<target name="help">
<echo>You can use the following targets:</echo>
<echo>
</echo>
<echo> help : (default) Prints this message </echo>
<echo> all : Cleans, compiles, and packages application</echo>
<echo> clean : Deletes work directories</echo>
<echo> compile : Compiles servlets into class files</echo>
<echo> dist : Packages artifacts into a deployable WAR</echo>
<echo>
</echo>
<echo>For example, to clean, compile, and package all at once, run:</echo>
<echo>prompt> ant all </echo>
</target>
<!-- Define the CLASSPATH -->
<path id="compile.classpath">
<fileset dir="${tcserver.home}/bin">
<include name="*.jar" />
</fileset>
<pathelement location="${tcserver.home}/lib" />
<fileset dir="${tcserver.home}/lib">
<include name="*.jar" />
</fileset>
</path>
<target name="all" depends="clean,compile,dist" description="Clean work dirs, then compile and create a WAR" />
<target name="clean" description="Delete old work and dist directories">
<delete dir="${work.home}" />
<delete dir="${dist.home}" />
</target>
<target name="prepare" depends="clean" description="Create working dirs and copy static files to work dir">
<mkdir dir="${dist.home}" />
<mkdir dir="${work.home}/WEB-INF/classes" />
<!-- Copy static HTML and JSP files to work dir -->
<copy todir="${work.home}">
<fileset dir="${web.home}" />
</copy>
</target>
<target name="compile" depends="prepare" description="Compile Java sources and copy to WEB-INF/classes dir">
<javac srcdir="${src.home}" destdir="${work.home}/WEB-INF/classes">
<classpath refid="compile.classpath" />
</javac>
<copy todir="${work.home}/WEB-INF/classes">
<fileset dir="${src.home}" excludes="**/*.java" />
</copy>
</target>
<target name="dist" depends="compile" description="Create WAR file for binary distribution">
<jar jarfile="${dist.home}/${app.name}-${app.version}.war" basedir="${work.home}" />
</target>
Now I included log4j as a local dependency and want to include it when I create my .war file. However, ANT is not able to find the dependency. Is there a way to get it working? Sorry for the basic question, I am a noob at it.
Update (and thanks for the help I got already):
I didn't want to add the "war" thing so I modified my build.xml as follows:
``
<target name="help">
<echo>You can use the following targets:</echo>
<echo>
</echo>
<echo> help : (default) Prints this message </echo>
<echo> all : Cleans, compiles, and packages application</echo>
<echo> clean : Deletes work directories</echo>
<echo> compile : Compiles servlets into class files</echo>
<echo> dist : Packages artifacts into a deployable WAR</echo>
<echo>
</echo>
<echo>For example, to clean, compile, and package all at once, run:</echo>
<echo>prompt> ant all </echo>
</target>
<!-- Define the CLASSPATH -->
<path id="compile.classpath">
<fileset dir="${tcserver.home}/bin">
<include name="*.jar" />
</fileset>
<pathelement location="${tcserver.home}/lib" />
<fileset dir="${tcserver.home}/lib">
<include name="*.jar" />
</fileset>
<fileset dir="${lib.dir}">
<include name="*.jar" />
</fileset>
</path>
<target name="all" depends="clean,compile,dist" description="Clean work dirs, then compile and create a WAR" />
<target name="clean" description="Delete old work and dist directories">
<delete dir="${work.home}" />
<delete dir="${dist.home}" />
</target>
<target name="prepare" depends="clean" description="Create working dirs and copy static files to work dir">
<mkdir dir="${dist.home}" />
<mkdir dir="${work.home}/WEB-INF/classes" />
<!-- Copy static HTML and JSP files to work dir -->
<copy todir="${work.home}">
<fileset dir="${web.home}" />
</copy>
</target>
<target name="compile" depends="prepare" description="Compile Java sources and copy to WEB-INF/classes dir">
<javac srcdir="${src.home}" destdir="${work.home}/WEB-INF/classes">
<classpath refid="compile.classpath" />
</javac>
<copy todir="${work.home}/WEB-INF/classes">
<fileset dir="${src.home}" excludes="**/*.java" />
<fileset dir="${lib.dir}" includes="*.jar" />
</copy>
</target>
<target name="dist" depends="compile" description="Create WAR file for binary distribution">
<jar jarfile="${dist.home}/${app.name}-${app.version}.war" basedir="${work.home}" />
</target>
Now ANT can find the dependencies and compile it. However when I deploy it to a Tomcat server, it fails to file the dependencies. Can you please provide some ideas as to how I can package the dependencies so its visible to Tomcat as well?
You should use the war task instead:
<war destfile="${warname}" webxml="war/WEB-INF/web.xml">
<fileset dir="${work.home}"/>
<lib dir="${lib.dir}/" includes="log4j.jar"/>
<classes dir = "${CLASSES}" />
</war>
You can configure it to find the dependency jars to be included in your war file and to specify the path to the web.xml folder. You shouldn't be coping the .class files to the destination in the war folder, let the war task do it for you.

apache ant - No source files and no packages have been specified

I tried to learned apache ant by this tutorial Apache Ant - tutorial
but after I created step by step build file and run it throws:
compile:
[javac] /home/nazar_art/workspace/de.vogella.build.ant.first/src/test/build.xml:27: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds
jar:
[jar] Building MANIFEST-only jar: /home/nazar_art/workspace/de.vogella.build.ant.first/src/test/dist/de.vogella.build.test.ant.jar
docs:
BUILD FAILED
/home/nazar_art/workspace/de.vogella.build.ant.first/src/test/build.xml:34: No source files and no packages have been specified.
I can't understand why this happened?
I use Ubuntu 12.04 OS, and Eclipse Indigo.
build.xml:
<?xml version="1.0"?>
<project name="Ant-Test" default="main" basedir=".">
<!-- Sets variables which can later be used. -->
<!-- The value of a property is accessed via ${} -->
<property name="src.dir" location="src" />
<property name="build.dir" location="bin" />
<property name="dist.dir" location="dist" />
<property name="docs.dir" location="docs" />
<!-- Deletes the existing build, docs and dist directory-->
<target name="clean">
<delete dir="${build.dir}" />
<delete dir="${docs.dir}" />
<delete dir="${dist.dir}" />
</target>
<!-- Creates the build, docs and dist directory-->
<target name="makedir">
<mkdir dir="${build.dir}" />
<mkdir dir="${docs.dir}" />
<mkdir dir="${dist.dir}" />
</target>
<!-- Compiles the java code (including the usage of library for JUnit -->
<target name="compile" depends="clean, makedir">
<javac srcdir="${src.dir}" destdir="${build.dir}">
</javac>
</target>
<!-- Creates Javadoc -->
<target name="docs" depends="compile">
<javadoc packagenames="src" sourcepath="${src.dir}" destdir="${docs.dir}">
<!-- Define which files / directory should get included, we include all -->
<fileset dir="${src.dir}">
<include name="**" />
</fileset>
</javadoc>
</target>
<!--Creates the deployable jar file -->
<target name="jar" depends="compile">
<jar destfile="${dist.dir}\de.vogella.build.test.ant.jar" basedir="${build.dir}">
<manifest>
<attribute name="Main-Class" value="test.Main" />
</manifest>
</jar>
</target>
<target name="main" depends="compile, jar, docs">
<description>Main target</description>
</target>
</project>
Code part:
package math;
public class MyMath {
public int multi(int number1, int number2) {
return number1 * number2;
}
}
package test;
import math.MyMath;
public class Main {
public static void main(String[] args) {
MyMath math = new MyMath();
System.out.println("Result is: " + math.multi(5, 10));
}
}
How to solve this trouble?
In your ANT build.xml the <docs> target is trying to generate documentation for "src" package. Try with the correct package name like "math" in <javadoc packagenames="src" sourcepath="..."> like packagenames="math,test". You can read the ANT documentation which has examples of javadoc task usage.

problem running JUnit tests with Ant in Eclipse. Beginner question

I'm learning these days how to use ant to run automated test folowing this tutorial.
I have JUnit in the classpath of my project. All seem to work fine and I can include it in my classes:
import junit.framework.TestCase; //line20
public class SimpleLattice1DTest extends TestCase{
...
}
My build.xml is:
<?xml version="1.0"?>
<project name="Ant-Test" default="compile" basedir=".">
<!-- Sets variables which can later be used. -->
<!-- The value of a property is accessed via ${} -->
<property name="src.dir" location="." />
<property name="build.dir" location="build" />
<property name="dist.dir" location="dist" />
<property name="docs.dir" location="docs" />
<property name="test.dir" location="jlife/tests" />
<property name="test.report.dir" location="test/report" />
<!-- Deletes the existing build, docs and dist directory-->
<target name="clean">
<delete dir="${build.dir}" />
<delete dir="${docs.dir}" />
<delete dir="${dist.dir}" />
</target>
<!-- Creates the build, docs and dist directory-->
<target name="makedir">
<mkdir dir="${build.dir}" />
<mkdir dir="${docs.dir}" />
<mkdir dir="${dist.dir}" />
<mkdir dir="${test.report.dir}" />
</target>
<!-- Compiles the java code (including the usage of library for JUnit -->
<target name="compile" depends="clean, makedir">
<javac srcdir="${src.dir}" destdir="${build.dir}">
</javac>
</target>
<!-- Creates Javadoc -->
<target name="docs" depends="compile">
<javadoc packagenames="src" sourcepath="${src.dir}" destdir="${docs.dir}">
<!-- Define which files / directory should get included, we include all -->
<fileset dir="${src.dir}">
<include name="**" />
</fileset>
</javadoc>
</target>
<!--Creates the deployable jar file -->
<target name="jar" depends="compile">
<jar destfile="${dist.dir}\CoreTest.jar" basedir="${build.dir}">
<manifest>
<attribute name="Main-Test" value="test.CoreTest" />
</manifest>
</jar>
</target>
<!-- Run the JUnit Tests -->
<!-- Output is XML, could also be plain-->
<target name="junit" depends="compile">
<junit printsummary="on" fork="true" haltonfailure="yes">
<formatter type="xml" />
<batchtest todir="${test.report.dir}">
<fileset dir="${src.dir}">
<include name="**/*Test*.java" />
</fileset>
</batchtest>
</junit>
</target>
</project>
When i run it into eclipse I get the following error:
[javac] C:\Documents and
Settings\noname\Documenti\JLife_git\JLife_git\JLife\src\jlife\tests\SimpleLattice1DTest.java:20:
package junit.framework does not exist
[javac] import junit.framework.TestCase;
I suppose there's something wrong with it, but I have no idea. Could someone put me in the right direction?
Your javac target doesn't specify anything apart from the source and target directory - it doesn't add any classpath entries; you'll need to add an entry for the appropriate JUnit jar file. See the javac task documentation for more details. You may want to specify the path to JUnit as a classpath attribute, a nested element, or a reference to a path declared elsewhere.
The eclipse classpath is separate from your ant environment. In your build file, when you call javac you need to supply a classpath attribute.
You can define the classpath at the top of the file with the rest of your properties, like this:
<path id="classpath">
<fileset dir="[path to libraries]" includes="**/*.jar" />
</path>
and then use it in each call to javac by setting the classpathref attribute, like this:
<javac srcdir="${src.dir}" destdir="${build.dir}" classpathref="classpath" />
You need to specify the directory that contains your .class files and your external jars (like junit).
e.g.
<!-- Populates a class path containing our classes and jars -->
<path id="dist.classpath">
<fileset dir="${lib}"/>
<pathelement path="${build}"/>
</path>
<!-- Compile the java code place into ${build} -->
<target name="compile" depends="-dirty" description="Compile the source.">
<javac srcdir="${source}" destdir="${build}" includeantruntime="false">
<classpath refid="dist.classpath"/>
<exclude name="${test.relative}/**/*"/>
</javac>
</target>
Here's the complete file I took that excerpt from in case you need ideas for how to setup other common things (emma, javadoc, etc)
<project name="imp" default="dist" basedir="..">
<description>Buildscript for IMP</description>
<property name="source" location="src"/>
<property name="lib" location="lib"/>
<property name="history" location="test_history"/>
<property name="web-tests" location="/var/www/tests"/>
<property name="web-files" location="/var/www/files"/>
<property name="web-javadoc" location="/var/www/javadoc"/>
<property name="web-emma" location="/var/www/emma"/>
<property name="emma.dir" value="${lib}"/>
<property name="test" location="${source}/imp/unittest"/>
<property name="test.relative" value="imp/unittest"/>
<property name="javadoc-theme" value="tools/javadoc-theme"/>
<!-- directories for generated files -->
<property name="build" location="build"/>
<property name="build-debug" location="debug"/>
<property name="build-coverage" location="coverage"/>
<property name="dist" location="dist"/>
<property name="reports" location="reports"/>
<property name="coverage-emma" location="${reports}/coverage/emma"/>
<!-- Populates a class path containing our classes and jars -->
<path id="dist.classpath">
<fileset dir="${lib}"/>
<pathelement path="${build}"/>
</path>
<path id="debug.classpath">
<fileset dir="${lib}"/>
<pathelement path="${build-debug}"/>
</path>
<!-- import emma. This classpath limits the coverage to just our classes -->
<path id="debug.imp.classpath">
<pathelement path="${build-debug}"/>
</path>
<taskdef resource="emma_ant.properties" classpathref="debug.classpath"/>
<!--
Shouldn't ever need to use this from the command line. IRC saith that the "private"
internal use only sort of targets are prefixed with '-'.
dirty because it's the opposite of the 'clean' target.
-->
<target name="-dirty">
<tstamp/>
<mkdir dir="${build}"/>
<mkdir dir="${build-debug}"/>
<mkdir dir="${build-coverage}"/>
<mkdir dir="${dist}"/>
<mkdir dir="${reports}"/>
<mkdir dir="${coverage-emma}"/>
</target>
<!-- clean up all the generated files and direcories -->
<target name="clean" description="Deletes all files and directories created by this script.">
<delete dir="${build}"/>
<delete dir="${build-debug}"/>
<delete dir="${build-coverage}"/>
<delete dir="${dist}"/>
<delete dir="${reports}"/>
<delete dir="${coverage-emma}"/>
</target>
<!-- Compile the java code place into ${build} -->
<target name="compile" depends="-dirty" description="Compile the source.">
<javac srcdir="${source}" destdir="${build}" includeantruntime="false">
<classpath refid="dist.classpath"/>
<exclude name="${test.relative}/**/*"/>
</javac>
</target>
<!-- Compile the java code with debug info place into ${build} -->
<target name="compile-debug" depends="-dirty" description="Compile the source with debug information.">
<javac
srcdir="${source}"
destdir="${build-debug}"
includeantruntime="false"
debug="true"
debuglevel="lines,vars,source"
>
<classpath refid="debug.classpath"/>
</javac>
</target>
<!-- roll up everyting into a single jar file -->
<target name="dist" depends="clean, compile" description="Generate the distribution file for IMP.">
<!-- Copy the library .jars to the directory where the IMP distribution will be located -->
<copy todir="${dist}">
<fileset dir="${lib}"/>
</copy>
<!-- TODO: Generate the MANIFEST.MF file on the fly -->
<jar jarfile="${dist}/imp.jar" basedir="${build}" manifest="tools/MANIFEST.MF"/>
<!-- dump to web server -->
<copy todir="${web-files}">
<fileset dir="${dist}"/>
</copy>
</target>
<!-- build and run the tests then report the results in HTML -->
<target name="test" depends="compile-debug" description="Run all the JUnit tests and outputs the results as HTML.">
<!-- run the tests -->
<junit printsummary="true" haltonerror="false" haltonfailure="false">
<classpath refid="debug.classpath"/>
<formatter type="xml"/>
<batchtest fork="true" todir="${reports}">
<fileset dir="${source}">
<include name="${test.relative}/**/*Test*.java"/>
<exclude name="${test.relative}/**/AllTests.java"/>
</fileset>
</batchtest>
</junit>
<!-- report the results -->
<junitreport todir="${reports}">
<fileset dir="${reports}" includes="TEST-*.xml"/>
<report todir="${reports}"/>
</junitreport>
<!-- update the latest results file to be commited -->
<copy file="${reports}/TESTS-TestSuites.xml" tofile="${history}/test-results-latest.xml"/>
<!-- dump to webserver -->
<copy todir="${web-tests}">
<fileset dir="${reports}"/>
</copy>
</target>
<!-- run emma code coverage tool and publish results in HTML -->
<target name="emma" depends="compile-debug" description="Checks code coverage with Emma.">
<!-- put the magic emma juice into the classes -->
<emma>
<instr
instrpathref="debug.imp.classpath"
destdir="${coverage-emma}/instr"
metadatafile="${coverage-emma}/metadata.emma"
merge="true"
/>
</emma>
<!-- run the tests -->
<junit fork="true" printsummary="true" haltonerror="false" haltonfailure="false">
<classpath>
<pathelement location="${coverage-emma}/instr"/>
<path refid="debug.classpath"/>
</classpath>
<batchtest fork="true" todir="${reports}">
<fileset dir="${source}">
<include name="${test.relative}/**/*Test*.java"/>
<exclude name="${test.relative}/**/AllTests.java"/>
</fileset>
</batchtest>
<jvmarg value="-Demma.coverage.out.file=${coverage-emma}/coverage.emma"/>
<jvmarg value="-Demma.coverage.out.merge=true"/>
</junit>
<!-- publish the coverage report -->
<emma>
<report sourcepath="${source}" verbosity="verbose">
<fileset dir="${coverage-emma}">
<include name="*.emma"/>
</fileset>
<html outfile="${web-emma}/index.html"/>
</report>
</emma>
</target>
<!-- publish javadoc -->
<target name="javadoc" description="Creates javadoc for IMP.">
<delete dir="${web-javadoc}"/>
<javadoc
sourcepath="${source}"
defaultexcludes="no"
destdir="${web-javadoc}"
author="true"
version="true"
use="true"
windowtitle="IMP: Integrated Mechanisms Program"
overview="${source}/overview.html"
classpathref="debug.classpath"
stylesheetfile="${javadoc-theme}/stylesheet.css"
/>
<copy file="${javadoc-theme}/javadoc.jpg" tofile="${web-javadoc}/javadoc.jpg"/>
</target>
<target name="all" description="Runs test, emma, javadoc, and dist targets.">
<antcall target="test"/>
<antcall target="emma"/>
<antcall target="javadoc"/>
<antcall target="dist"/>
</target>
</project>
If you observe the error stack, you will find the following line, just above the error line you mentioned...
[javac] [search path for class files: C:\Program Files\Java\jre6\lib\resource...
This line shows all the jars available in the class path for this ant target execution.
You will definitely not find the desired jar over here i.e. junit-x.x.x.jar (junit-4.8.2.jar)
Now go to eclipse -> Window -> preferences -> Ant -> Runtime -> Global Entries -> Add Jars add junit-4.8.2jar (which you will find in your project lib directory)
If you play around the Ant -> Runtime -> classpath and the classpath related error line in the error stack, you will understand the issue.
Hope this solves your problem.

Cannot find Java Import in Eclipse?

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>
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.

Categories