0

I added commons-codec.1.2.jar to my Java Build Path

    [javac] C:\Users\souzamor\workspace\tczip\src\tczip\Tczip.java:190: error: c
annot find symbol
    [javac]   mdEnc = new String( Hex.encodeHex( diges
t ));
    [javac]                       ^

and here is my build.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<project>

    <target name="clean">
        <delete dir="build" />
    </target>

    <target name="compile">
        <mkdir dir="build/classes" />
        <javac srcdir="src" destdir="build/classes" />
    </target>

    <target name="jar">
        <mkdir dir="build/jar" />
        <jar destfile="build/jar/Tczip.jar" basedir="build/classes">
            <manifest>
                <attribute name="Main-Class" value="tczip.ZipComparison" />
            </manifest>
        </jar>
    </target>

    <target name="run">
        <java jar="build/jar/Tczip.jar" fork="true" />
    </target>

</project>

How could I add the jar files into the build.xml file?? I also have another Java class called Tczip which processes MD5:

 [java] Processing: bhmcommonclient.zip
 [java] Exception in thread "main" java.lang.NoClassDefFoundError: org/apach
mmons/codec/binary/Hex
 [java]     at tczip.Tczip.digest(Unknown Source)
 [java]     at tczip.Tczip.execute(Unknown Source)
 [java]     at tczip.ZipComparison.showFiles(Unknown Source)
 [java]     at tczip.ZipComparison.showFiles(Unknown Source)
 [java]     at tczip.ZipComparison.showFiles(Unknown Source)
 [java]     at tczip.ZipComparison.showFiles(Unknown Source)
 [java]     at tczip.ZipComparison.showFiles(Unknown Source)
 [java]     at tczip.ZipComparison.matchMD5(Unknown Source)
 [java]     at tczip.ZipComparison.main(Unknown Source)

I don't think I'm adding that correctly to my jar file ... how could I do that? I'm totally new to Ant

cybertextron
  • 10,547
  • 28
  • 104
  • 208

1 Answers1

5

First declare it like this:

<path id="external.classpath">
    <pathelement location="${lib.dir}/commons-codec-1.2.jar"/>
</path>

Then, inside your javac element, include it in the classpath like this:

  <classpath>
    <path refid="external.classpath" />
  </classpath>
Dan D.
  • 32,246
  • 5
  • 63
  • 79
  • There was a typo, I was refering to 1.1, I fixed it now. Make sure ${lib.dir} refers you to the right place where the jar is. – Dan D. Aug 30 '12 at 16:40
  • So does it fail on compiling or on run now? – Dan D. Aug 30 '12 at 16:45
  • yes, It fails, because I'm not compiling the other class. How can I make the other class to be compiled and add it to the `jar` file? – cybertextron Aug 30 '12 at 16:48
  • Where? When you compile your code with javac or when you try to run? If your code uses that library, you must add it in the classpath as I showed you, otherwise your code will not compile. I am not asking you to compile the commons jar. – Dan D. Aug 30 '12 at 16:50
  • when I do `ant compile`. There are two classes: `ZipComparison` and `Tczip` ... `Tczip` processes `MD5`, while `ZipComparison` checks for files with different `MD5` ... so `ZipComparison` calls `Tczip`, but I'm getting that error... I guess I'm not compiling `Tczip` in the `build.xml` file... – cybertextron Aug 30 '12 at 16:53
  • what "javac element"? – ysth Jan 07 '14 at 01:12