ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Geir Magnusson Jr." <>
Subject Re: Classloader question
Date Tue, 09 Oct 2001 13:24:03 GMT
Included is code / xml that gets to the essence of the problem.  I am hoping
I am being a dolt, and there is a simple, easy way to solve this.  If that
is the case, use the following as a template for the response.  Don't be shy

"No, you moron, all you have to do is [FILL IN HERE].  Read the manual when
we get one."


Really - don't be shy - I am not a very hip ant user, so I assume I am just
overlooking the obvious.

Anyway, the idea is this.

1) Make a jar "foo.jar" containing a file "" that contains whatever.
It doesn't matter.

2) here is the build.xml

<?xml version="1.0"?>

<project name="main" default="foo">

  <!-- Build classpath -->
  <path id="classpath">
    <pathelement location="./"/>
  <target name="foo">
    <taskdef name="foo" classname="TestTask">
        <pathelement location="./foo.jar"/>
        <path refid="classpath"/>
    <foo foo="" />



3) Here is the code - keep it in the same directory as build.xml and foo.jar


public class TestTask
    extends Task

    public void setFoo( String foo )
        throws Exception
        ClassLoader classLoader = this.getClass().getClassLoader();
            InputStream inputStream = classLoader.getResourceAsStream( foo
             if (inputStream == null)
                throw new Exception("no foo!");
         catch (IOException ioe)
            throw new Exception(" io exception" );
   public void execute()
        throws BuildException

4) Compile code, type 'ant'.  Works fine under ant 1.3 and ant 1.4.

5) Now, to show the problem, simply put the current directory in the
classpath.  Under tcsh under OS X :  setenv CLASSPATH ./

Now try again.  Works under 1.3, but *not* under 1.4.  It can't find it.

I know that this is what we have been talking about - but I needed to show
that Velocity, factories, singleton pattern, etc have nothing to do with it.

I don't think that this is particularly esoteric.  Further, I think that
people will stumble across the problem of having the jar (or class)
containing the Task in the classpath.

The pattern of getting the classloader and then getting the resource is only
in one place, and can be modified as need be.  That's what I am looking to

Thanks for all the help so far.


Geir Magnusson Jr.
System and Software Consulting
"Whoever would overthrow the liberty of a nation must begin by subduing the
freeness of speech." - Benjamin Franklin

View raw message