You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@ant.apache.org by Edoardo Vacchi <un...@gmail.com> on 2013/03/26 18:25:46 UTC

Ant System Class Loader does not honor $CLASSPATH, honors $LOCALCLASSPATH

Hi to everybody on the list,

I am forwarding the question I've asked on stackoverflow
http://stackoverflow.com/questions/15383099/ant-system-class-loader-does-not-honor-classpath-honors-localclasspath
as I did not have any feedback in several days. I hope I will find an
answer here.

Thanks in advance

------

I am implementing an ant task as a wrapper for another class, which
loads other several classes using the system class loader. Now, the
task is in the same jar of these other classes, so I wonder why it is
not finding them, since the task *is* running

Please notice that my classes are in the `$CLASSPATH` env variable.
The problem will not occur if I `export LOCALCLASSPATH=$CLASSPATH`

Minimal example:


    <taskdef name="mytask" classname="my.package.MyTask"  />

    <target name="compile">
        <mytask />
    </target>


you can easily see the problem here

    public class MyTask extends Task {
       public void execute() throws BuildException {
        try {
            ClassLoader cl = ClassLoader.getSystemClassLoader();

            // this will only print the ant jar file path
            for(URL url: ((URLClassLoader)cl).getURLs()){
        	log(url.getFile());
            }

            cl.loadClass("my.package.OtherClass"); // throws an exception

        } catch (Exception ex) {
            throw new BuildException(ex);
        }
      }

    }

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


Re: Ant System Class Loader does not honor $CLASSPATH, honors $LOCALCLASSPATH

Posted by Edoardo Vacchi <un...@gmail.com>.
Ok, I think I've found my problem. The fact is the system classloader
is never updated, but the context classloader is. So, all I have to do
is to substitute any instance of

    ClassLoader cl = ClassLoader.getSystemClassLoader();

with

    ClassLoader cl = Thread.currentThread().getContextClassLoader();

The context class loader seems to actually honor -lib and $CLASSPATH


On Wed, Mar 27, 2013 at 9:07 AM, Edoardo Vacchi
<un...@gmail.com> wrote:
> On Tue, Mar 26, 2013 at 11:43 PM, Rainer Noack <ra...@noacks.net> wrote:
>
>> if you're launching ant via shell script, it is using
>> oata.launcher.Launcher.java
>>
>> This class reorganises the classpath a bit.
>
> [...]
>
> Hi Rainer,
> then how can I pass to the new ClassLoader a custom classpath? (which
> is in fact the path(s) to the jar(s) that contains the taskdef'd
> task?)
>
> e.v.

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


Re: Ant System Class Loader does not honor $CLASSPATH, honors $LOCALCLASSPATH

Posted by Edoardo Vacchi <un...@gmail.com>.
On Tue, Mar 26, 2013 at 11:43 PM, Rainer Noack <ra...@noacks.net> wrote:

> if you're launching ant via shell script, it is using
> oata.launcher.Launcher.java
>
> This class reorganises the classpath a bit.

[...]

Hi Rainer,
then how can I pass to the new ClassLoader a custom classpath? (which
is in fact the path(s) to the jar(s) that contains the taskdef'd
task?)

e.v.

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


Re: Ant System Class Loader does not honor $CLASSPATH, honors $LOCALCLASSPATH

Posted by Rainer Noack <ra...@noacks.net>.
Hi Edoardo,

if you're launching ant via shell script, it is using 
oata.launcher.Launcher.java

This class reorganises the classpath a bit.
The env variable CLASSPATH and the classpath commandline argument are 
stripped and replaced by the minimum classpath used to launch ant.
A child classloader of the system classloader is created with the 
original CLASSPATH entries.
The oata.Project is then loaded with this classloader.

Hope that helps

Rainer


Am 26.03.2013 18:25, schrieb Edoardo Vacchi:
> Hi to everybody on the list,
>
> I am forwarding the question I've asked on stackoverflow
> http://stackoverflow.com/questions/15383099/ant-system-class-loader-does-not-honor-classpath-honors-localclasspath
> as I did not have any feedback in several days. I hope I will find an
> answer here.
>
> Thanks in advance
>
> ------
>
> I am implementing an ant task as a wrapper for another class, which
> loads other several classes using the system class loader. Now, the
> task is in the same jar of these other classes, so I wonder why it is
> not finding them, since the task *is* running
>
> Please notice that my classes are in the `$CLASSPATH` env variable.
> The problem will not occur if I `export LOCALCLASSPATH=$CLASSPATH`
>
> Minimal example:
>
>
>      <taskdef name="mytask" classname="my.package.MyTask"  />
>
>      <target name="compile">
>          <mytask />
>      </target>
>
>
> you can easily see the problem here
>
>      public class MyTask extends Task {
>         public void execute() throws BuildException {
>          try {
>              ClassLoader cl = ClassLoader.getSystemClassLoader();
>
>              // this will only print the ant jar file path
>              for(URL url: ((URLClassLoader)cl).getURLs()){
>          	log(url.getFile());
>              }
>
>              cl.loadClass("my.package.OtherClass"); // throws an exception
>
>          } catch (Exception ex) {
>              throw new BuildException(ex);
>          }
>        }
>
>      }
>
> ---------------------------------------------------------------------
> 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