0

I have two classes, ZipComparison and Tczip. Tczip digests a zip file and processes its MD5, while ZipComparison searches for .zip entries, and compares two different versions of the .zip file to determine if they have the same content. For example, in a package_a there is encodes.zip so I want to determine if in the package_b the encodes.zip is the same as the .zip file in package_a. So I process the MD5, and if the they match, then I don't need to copy that MD5. The build.xml is below:

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

    <path id="external.classpath">
        <pathelement location="src/commons-codec-1.2.jar"/>
    </path>

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

    <target name="compile">
        <mkdir dir="build/classes" />
        <javac srcdir="src/tczip" destdir="build/classes">
            <classpath>
                <path refid="external.classpath" />
             </classpath>
        </javac>
    </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>

So I'm trying to create an ant build file. ZipComparison utilizes Tczip, but when I compile, the execution is perfect, however, when I do ant run I get the following error:

C:\Users\souzamor\workspace\tczip>ant run
Buildfile: C:\Users\souzamor\workspace\tczip\build.xml

run:
     [java] Processing: bhmcommonclient.zip
     [java] Exception in thread "main" java.lang.NoClassDefFoundError: org/apach
e/commons/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)
     [java] Caused by: java.lang.ClassNotFoundException: org.apache.commons.code
c.binary.Hex
     [java]     at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
     [java]     at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
     [java]     at java.security.AccessController.doPrivileged(Native Method)
     [java]     at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
     [java]     at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
     [java]     at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)

     [java]     at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
     [java]     ... 9 more
     [java] Java Result: 1

BUILD SUCCESSFUL
Total time: 4 seconds

I know Tczip is missing on the compile stage, but how can I add that along ZipComparison so I don't have that kind of error ? Thanks

cybertextron
  • 10,547
  • 28
  • 104
  • 208

1 Answers1

0

It looks like the tczip.Tczip class is looking for a class from Apache Commons Codec. Make sure that this jar is on your classpath along with any other dependencies. See this answer to see how to add the classpath to your JAR's manifest when you create your JAR file from an Ant <path/> element.

Also, your XML doesn't use the depends tag to make sure that compile and jar are executed with run, and that's not good build script practice. It should really use depends:

<target name="clean">
    <!-- ... -->
</target>

<target name="compile">
    <!-- ... -->
</target>

<target name="jar" depends="compile">
    <!-- ... -->
</target>

<target name="run" depends="jar">
    <!-- ... -->
</target>
Community
  • 1
  • 1
Brian
  • 17,079
  • 6
  • 43
  • 66