ant: ftp task failing due to NoClassDefFoundError
Asked Answered
A

5

7

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.

Alyss answered 24/5, 2013 at 17:55 Comment(0)
A
21

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.

Alyss answered 24/5, 2013 at 19:3 Comment(1)
In my case, ant home was on: /usr/local/Cellar/ant/1.10.5/ (MacOS ElCapitan)Multipara
R
3

According to https://ant.apache.org/manual/Tasks/ftp.html, you need to install Apache Commons Net and Jakarta Oro.

Copy these jars to your \lib directory, as Jason Posit said.

If you are using Eclipse, you might need to update the Ant buildpath. Go to "Preferences" > "Ant" > "Runtime" > "Add external JARs" to add these libraries to your Ant buildpath.

Rashidarashidi answered 24/1, 2016 at 15:44 Comment(2)
How to do this in IntelliJ?Prolactin
In IntelliJ, you can also add additional libraries, either on global level or on project level. See jetbrains.com/help/idea/library.htmlRashidarashidi
G
1

If you use a Debian Linux distribution or Raspbian on a Raspberry Pi, you can simply solve this problem by running:

sudo apt-get install libcommons-net-java
Gerous answered 10/12, 2015 at 18:0 Comment(0)
J
0

Had the same problem. It happened to me when I was using ant-1.9.1 instead of ant-1.8.2.

Switching the ant version fixed it.

Jam answered 9/2, 2016 at 12:57 Comment(0)
R
0

Steps which worked For me

Navigate to folder where Jenkins is installed

jenkins>>tools>>hudson.tasks.Ant_AntInstallation>>(ANT VERSION Installed)>>lib

place the below Files & restart Jenkins

  1. commons-net-3.3.jar
  2. commons-net-3.4.jar
  3. jakarta-oro.jar
Recommendatory answered 5/4, 2016 at 6:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.