CPDTask has a NoClassDefFoundError for FilenameUtils

540 views Asked by At

After updating from PMD 5.0.3 to 5.0.5, I am getting a NoClassDefFoundError when trying to run CPD via ant. I see that CPD changed to use FilenameUtils, but that should not be a problem as I have commons-io.jar in the path for the task.

Here is the Ant target (FilenameUtils is in commons-io.jar)

   <target name="cpd" depends="init">
      <path id="pmd.cpd.classpath">
         <pathelement location="${3rdparty_libdir}/pmd.jar"/>
         <pathelement location="${3rdparty_libdir}/asm.jar"/>
         <pathelement location="${3rdparty_libdir}/commons-io.jar"/>
         <pathelement location="${3rdparty_libdir}/jaxen.jar"/>
      </path>

      <taskdef name="cpd" classname="net.sourceforge.pmd.cpd.CPDTask" classpathref="pmd.cpd.classpath"/>

      <cpd minimumTokenCount="${pmd.cpd.minimumTokenCount}" format="${pmd.cpd.format}"
               outputFile="${pmd.cpd.outputFile}" encoding="${pmd.cpd.encoding}">
         <fileset dir="${srcdir}">
            <include name="**/*.java"/>
         </fileset>
      </cpd>
   </target>

I don't see any way to put the classpathref in the cpd call.

Here is the error

java.lang.NoClassDefFoundError: org/apache/commons/io/FilenameUtils
        at net.sourceforge.pmd.cpd.CPD.add(CPD.java:100)
        at net.sourceforge.pmd.cpd.CPD.add(CPD.java:60)
        at net.sourceforge.pmd.cpd.CPDTask.tokenizeFiles(CPDTask.java:119)
        at net.sourceforge.pmd.cpd.CPDTask.execute(CPDTask.java:64)
        at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:291)
        ... snip
Caused by: java.lang.ClassNotFoundException: org.apache.commons.io.FilenameUtils
        at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
        ... 20 more
2

There are 2 answers

0
Nickolai Astashonok On

Replace:

  <path id="pmd.cpd.classpath">
     <pathelement location="${3rdparty_libdir}/pmd.jar"/>
     <pathelement location="${3rdparty_libdir}/asm.jar"/>
     <pathelement location="${3rdparty_libdir}/commons-io.jar"/>
     <pathelement location="${3rdparty_libdir}/jaxen.jar"/>
  </path>

With:

 <path id="pmd.cpd.classpath">
        <fileset dir="${3rdparty_libdir}">
            <include name="**/*.jar" />
        </fileset>
    </path>

{3rdparty_libdir} should contain pmd, jaxen and commons-io jars. Asm isn't necessary.

0
MartinMoizard On

I managed to make it works using the following command line:

java -Xmx512m -classpath ~/pmd-bin-5.0-2.5/lib/commons-io-2.2.jar:~/pmd-bin-5.0-2.5/lib/asm-3.2.jar:~/pmd-bin-5.0-2.5/lib/jaxen-1.1.1.jar:~/pmd-bin-5.0-2.5/lib/pmd-5.0.5.jar:~/ObjCLanguage-0.0.8-SNAPSHOT.jar net.sourceforge.pmd.cpd.CPD --minimum-tokens 100 --files PATH_TO_XCODE_SOURCE --language ObjectiveC --encoding UTF-8 --format net.sourceforge.pmd.cpd.XMLRenderer