You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@ant.apache.org by Andre Dantas Rocha <an...@uol.com.br> on 2008/11/02 13:47:30 UTC

java.lang.NoClassDefFoundError in custom task

Hi all,

 

I'm trying to create a custom task that make some transformations using
Javassist (actually, this is my first Ant task). The task is very simple,
but can't figure out why it always produces a java.lang.NoClassDefFoundError
(since the corresponding jar is in the path as message shows).

 

I'm running inside Eclipse

 

The code is below, could anybody help me?

 

Thanks,

 

Andre

 

public class TransformerTask extends Task {

 

   private String transformerClassName = Transformer.class.getName();

   private Path classpath;

 

   public void setClasspath(Path classpath) {

      this.classpath = classpath;

   }

 

   public Path createClasspath() {

      if (this.classpath == null) {

         this.classpath = new Path(getProject());

      }

      return this.classpath.createPath();

   }

 

   public void setClasspathRef(Reference r) {

      createClasspath().setRefid(r);

   }

 

   public Path getClasspath() {

      return classpath;

   }

 

   private Transformer getTransformer() throws BuildException {

      Transformer transformer = null;

      try {

         Class<?> dc;

         if (classpath != null) {

            

            System.out.println("CLASSPATH: " + classpath);

            

            log("Loading " + transformerClassName

                  + " using AntClassLoader with classpath " + classpath,

                  Project.MSG_VERBOSE);

            AntClassLoader loader =
getProject().createClassLoader(classpath);

            dc = loader.loadClass(transformerClassName);

         } else {

            log("Loading " + transformerClassName

                  + " using system loader.", Project.MSG_VERBOSE);

            dc = Class.forName(transformerClassName);

         }

         transformer = (Transformer) dc.newInstance();

      } catch (ClassNotFoundException e) {

         throw new BuildException("Class Not Found: Transformer "

               + transformerClassName + " could not be loaded", e,

               getLocation());

      } catch (IllegalAccessException e) {

         throw new BuildException("Illegal Access: Transformer "

               + transformerClassName + " could not be loaded", e,

               getLocation());

      } catch (InstantiationException e) {

         throw new BuildException("Instantiation Exception: Transformer "

               + transformerClassName + " could not be loaded", e,

               getLocation());

      }

      return transformer;

   }

 

   public void execute() throws BuildException {

      Transformer transformer = getTransformer();

      transformer.transform(null);

   }

}

 

<project default="jeha">

   <path id="jeha.classpath">

      <fileset dir="lib" includes="*.jar"/>

   </path>

 

   <taskdef name="jeha"
classname="org.codecompany.jeha.util.TransformerTask"/>

 

   <target name="jeha">

      <jeha classpathref="jeha.classpath"/>

   </target>

</project>

 

 

Buildfile: C:\Andre\Projetos\Teste\build.xml

jeha:

     [jeha] CLASSPATH:
C:\Andre\Projetos\Teste\lib\ant.jar;C:\Andre\Projetos\Teste\lib\commons-logg
ing-1.0.4.jar;C:\Andre\Projetos\Teste\lib\javassist-3.9.jar;C:\Andre\Projeto
s\Teste\lib\log4j-1.2.15.jar;C:\Andre\Projetos\Teste\lib\retroweaver-all-2.0
.7.jar

 

BUILD FAILED

C:\Andre\Projetos\Teste\build.xml:9: java.lang.NoClassDefFoundError:
javassist/ClassPool

 

Total time: 203 milliseconds


RES: java.lang.NoClassDefFoundError in custom task

Posted by Andre Dantas Rocha <an...@uol.com.br>.
Hello Steve,

I realized that it was a classloader problem. I fixed the code using the
same strategy used in org.apache.tools.ant.taskdefs.ExecuteJava.

Thanks,

Andre



-----Mensagem original-----
De: Steve Loughran [mailto:stevel@apache.org] 
Enviada em: segunda-feira, 3 de novembro de 2008 11:30
Para: Ant Users List
Assunto: Re: java.lang.NoClassDefFoundError in custom task

Andre Dantas Rocha wrote:
> Hi all,
> 
>  
> 
> I'm trying to create a custom task that make some transformations using
> Javassist (actually, this is my first Ant task). The task is very simple,
> but can't figure out why it always produces a
java.lang.NoClassDefFoundError
> (since the corresponding jar is in the path as message shows).
> 
>  
> 
> I'm running inside Eclipse
> 
>  
> 
> The code is below, could anybody help me?


0. try outside eclipse first
1. have the exceptions include the classpath path in their text; it 
makes it easier to see what is wrong.
2. check you have the spellling of the class to load right;
3. check you have all other classes it needs on the classpath right too

-- 
Steve Loughran                  http://www.1060.org/blogxter/publish/5
Author: Ant in Action           http://antbook.org/

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


Re: java.lang.NoClassDefFoundError in custom task

Posted by Steve Loughran <st...@apache.org>.
Andre Dantas Rocha wrote:
> Hi all,
> 
>  
> 
> I'm trying to create a custom task that make some transformations using
> Javassist (actually, this is my first Ant task). The task is very simple,
> but can't figure out why it always produces a java.lang.NoClassDefFoundError
> (since the corresponding jar is in the path as message shows).
> 
>  
> 
> I'm running inside Eclipse
> 
>  
> 
> The code is below, could anybody help me?


0. try outside eclipse first
1. have the exceptions include the classpath path in their text; it 
makes it easier to see what is wrong.
2. check you have the spellling of the class to load right;
3. check you have all other classes it needs on the classpath right too

-- 
Steve Loughran                  http://www.1060.org/blogxter/publish/5
Author: Ant in Action           http://antbook.org/

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org