ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Geir Magnusson Jr." <ge...@optonline.net>
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."

Ok? 

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 "foo.data" 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="./"/>
  </path>
  
  <target name="foo">
    
    <taskdef name="foo" classname="TestTask">
      <classpath>
        <pathelement location="./foo.jar"/>
        <path refid="classpath"/>
      </classpath>
    </taskdef>
  
    <foo foo="foo.data" />

  </target>

</project>

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

import org.apache.tools.ant.Task;
import org.apache.tools.ant.BuildException;
import java.io.InputStream;
import java.io.IOException;

public class TestTask
    extends Task
{

    public void setFoo( String foo )
        throws Exception
    {
        ClassLoader classLoader = this.getClass().getClassLoader();
  
        try
        {
            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
fix.

Thanks for all the help so far.

geir


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



Mime
View raw message