ant: ftp task failing due to NoClassDefFoundError

Jason Posit picture Jason Posit · May 24, 2013 · Viewed 12.2k times · Source

I have been following some example source code but I am getting an error upon executing the ftp task. Here is my build.xml file:

?xml version="1.0"?>
<project name="HelloWorld" default="deploy">
  <target name="init">
    <mkdir dir="build/classes" />
    <mkdir dir="dist" />
  </target>
  <target name="compile" depends="init">
    <javac includeantruntime="false"
      srcdir="src"
      destdir="build/classes"/>
  </target>
  <target name="doc" depends="init">
    <javadoc destdir="build/classes"
      sourcepath="src"
      packagenames="org.*" />
  </target>
  <target name="package" depends="compile,doc">
    <jar destfile="dist/project.jar"
      basedir="build/classes" />
  </target>
  <taskdef name="ftp" classname="org.apache.tools.ant.taskdefs.optional.net.FTP">
    <classpath>
      <pathelement location="/usr/local/ant/lib/ant-commons-net.jar"/>
    </classpath>
  </taskdef>
  <target name="deploy" depends="package">
    <ftp classname="org.apache.tools.ant.taskdefs.optional.net.FTP"
      server="${server.name}"
      userid="${ftp.username}"
      password="${ftp.password}">
      <fileset dir="dist" />
    </ftp>
  </target>
</project>

Here are some settings:

$ ant -version
Apache Ant(TM) version 1.9.0 compiled on March 5 2013
$ echo $ANT_HOME
/usr/local/ant
$ ls -l $ANT_HOME
lrwxrwxrwx 1 root root 17 May 20 11:54 /usr/local/ant -> apache-ant-1.9.0/

Here is the output with the error I am getting. I thought ant build files were supposed to be portable. What is the problem here? Somehow the "Ant" FTP task looks for a class name which is coming from a class which is not distributed with ant. I tried pointing the task to the class distributed with ant but ant still does not seem to find the class corresponding to the ftp task which it is looking for.

Buildfile: /home/johndoe/ex/build.xml

init:

compile:

doc:
  [javadoc] Generating Javadoc
  [javadoc] Javadoc execution
  [javadoc] Loading source files for package org.example.helloworld...
  [javadoc] Constructing Javadoc information...
  [javadoc] Standard Doclet version 1.7.0_21
  [javadoc] Building tree for all the packages and classes...
  [javadoc] Building index for all the packages and classes...
  [javadoc] Building index for all classes...

package:
      [jar] Building jar: /home/johndoe/ex/dist/project.jar

deploy:

BUILD FAILED
/home/johndoe/ex/build.xml:30: Could not create type ftp due to java.lang.NoClassDefFoundError: org/apache/commons/net/ftp/FTPClientConfig
    at org.apache.tools.ant.taskdefs.optional.net.FTP$LanguageCode.getValidLanguageCodes(FTP.java:2698)
    at org.apache.tools.ant.taskdefs.optional.net.FTP$LanguageCode.<clinit>(FTP.java:2694)
    at org.apache.tools.ant.taskdefs.optional.net.FTP.<init>(FTP.java:137)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
    at org.apache.tools.ant.AntTypeDefinition.innerCreateAndSet(AntTypeDefinition.java:328)
    at org.apache.tools.ant.AntTypeDefinition.createAndSet(AntTypeDefinition.java:274)
    at org.apache.tools.ant.AntTypeDefinition.icreate(AntTypeDefinition.java:219)
    at org.apache.tools.ant.AntTypeDefinition.create(AntTypeDefinition.java:206)
    at org.apache.tools.ant.ComponentHelper.createComponent(ComponentHelper.java:285)
    at org.apache.tools.ant.ComponentHelper.createComponent(ComponentHelper.java:263)
    at org.apache.tools.ant.UnknownElement.makeObject(UnknownElement.java:417)
    at org.apache.tools.ant.UnknownElement.maybeConfigure(UnknownElement.java:163)
    at org.apache.tools.ant.Task.perform(Task.java:347)
    at org.apache.tools.ant.Target.execute(Target.java:435)
    at org.apache.tools.ant.Target.performTasks(Target.java:456)
    at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1393)
    at org.apache.tools.ant.Project.executeTarget(Project.java:1364)
    at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
    at org.apache.tools.ant.Project.executeTargets(Project.java:1248)
    at org.apache.tools.ant.Main.runBuild(Main.java:851)
    at org.apache.tools.ant.Main.startAnt(Main.java:235)
    at org.apache.tools.ant.launch.Launcher.run(Launcher.java:280)
    at org.apache.tools.ant.launch.Launcher.main(Launcher.java:109)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.net.ftp.FTPClientConfig
    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:423)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
    ... 26 more

Total time: 2 seconds

Ideally I shouldn't have to download other classes to get ant to work. It should work out of the box, otherwise it is not that portable. Plus, if someone could help me with where to download the classes that are missing it would be of great help.

Thanks.

Answer

Jason Posit picture Jason Posit · May 24, 2013

I was able to solve the problem by downloading commons-net-3.2-bin.tar.gz from http://commons.apache.org/proper/commons-net/download_net.cgi and installing commons-net-3.2.jar into my /usr/local/ant/lib folder.

Regards.