ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jful...@ruminate.co.uk
Subject Re: ANT Task classpath?
Date Fri, 03 Sep 2004 01:30:05 GMT
Guru Balse <guru.balse@oracle.com> wrote ..
> Hi Jim,
> 
> The problem is that setting the classpath within taskdef does not seem
> to work (at least not in ANT 1.6.0).  A few simple files I have that illustrate
> this are as follows:
> 
> C:\ant_learn
> |   TestCP.java		Task that instantiates a specified object (to test the
> classpath problem)
> |   test_cp.xml		Task driver
> \---test
>         Test.jar	Test jar file
>         Test.java	Test class - prints out Hello when constructed
> 
> c:/ant_learn/test_cp.xml contains
> ---------------------------------
> <?xml version="1.0"?>
> <project name="test" default="test">
>   <taskdef name="test_cp" classname="TestCP">
>     <classpath>
>       <fileset dir="c:/ant_learn/test">
>         <include name="Test.jar"/>
>       </fileset>
>     </classpath>
>   </taskdef>
>   <target name="test">
>     <test_cp createObject="Test" />
>   </target>
> </project>
> ---------------------------------
> c:/ant_learn/TestCP.java contains
> ---------------------------------
> import org.apache.tools.ant.*;
> public class TestCP extends Task {
>   private String createObject = null;
>   public void setCreateObject(String createObject) { this.createObject
> = createObject; }
>   public void execute() throws BuildException {
>     try {
>       if (createObject == null) {
>           log("Must set object to create!", Project.MSG_ERR);
>       } else {
> 	log("Creating object of type " + createObject, Project.MSG_INFO);
> 	(Class.forName(createObject)).newInstance();
>       }
>     } catch (Exception e) {
>       log("Exception encountered: " + e, Project.MSG_ERR);
>       throw new BuildException("Build failed!", e);
>     }
>   }
> }
> ---------------------------------
> c:/ant_learn/test/Test.java contains
> ---------------------------------
> public class Test {
>   public Test() {
>     System.out.println("Hello there!");
>   }
> }
> ---------------------------------
> 
> c:/ant_learn/test is NOT in my classpath, so when I run ant -f test_cp.xml,
> I get:
> 
> Buildfile: C:\ant_learn\test_cp.xml
> 
> test:
>   [test_cp] Creating object of type Test
>   [test_cp] Exception encountered: java.lang.ClassNotFoundException: Test
> 
> BUILD FAILED
> C:\ant_learn\test_cp.xml:11: Build failed!
> 
> Total time: 4 seconds
> 
> Tool completed successfully
> 
> BUT if I add c:/ant_learn/test to my CLASSPATH in a command prompt, I get:
> Buildfile: test_cp.xml
> 
> test:
>   [test_cp] Creating object of type Test
>   [test_cp] Hello there!

u are answering your own question ! your classpath only knows about Test.jar...nothing else....I
assume Test class is not in your jar...u need to add path ...u could do this by adding class
as below to your fileset

     <classpath>
       <fileset dir="c:/ant_learn/test">
         <include name="Test.jar"/>
         <include name="Test.class"/>
      </fileset>
     </classpath>

or adding a pathelement

   <classpath>
       <fileset dir="c:/ant_learn/test">
         <include name="Test.jar"/>
      </fileset>
     <pathelement location="c:/ant_learn/test"/>
   </classpath>

hth, Jim Fuller


Mime
View raw message