user1068636
user1068636

Reputation: 1939

The taskdef ant task cannot be found

All -

I'm following the most simplest instructions on this page:

http://ant.apache.org/manual/develop.html

However, when I try to execute the target "main" I get this error in netbeans:

taskdef class dec102012.MyAntTask cannot be found using the classloader AntClassLoader[]

But this error does not make sense because my new Java class that extends "Task" looks like this:

package dec102012;

import org.apache.tools.ant.BuildException;

public class MyAntTask extends org.apache.tools.ant.Task{
    private String msg;

    // The method executing the task
    public void execute() throws BuildException {
        System.out.println(msg);
    }

    // The setter for the "message" attribute
    public void setMessage(String msg) {
        this.msg = msg;
    }
}

The relevant portion in my build.xml looks like:

<taskdef name="mytask" classname="dec102012.MyAntTask" classpath="dec102012"/>

<target name="main">
    <mytask message="Hello World! MyVeryOwnTask works!"/>
</target>

Upvotes: 12

Views: 61551

Answers (1)

user1068636
user1068636

Reputation: 1939

The problem is the Ant Classloader needs to know where the *.class file sits.

Once I changed the build.xml to look like:

<taskdef name="mytask" classname="dec102012.MyAntTask" classpath="build/classes"/>

  <target name="main">
    <mytask message="Hello World! MyVeryOwnTask works!"/>
  </target>

it worked (i.e. it printed out the Hello World message).

Upvotes: 19

Related Questions