ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Joerg Heinicke <>
Subject Ant and thread context class loader
Date Tue, 24 Oct 2006 08:00:20 GMT

I have a problem with a library and an Ant task I wrote for it. Internally it
tries to get the thread context class loader to load a class:

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

Unfortunately, this does not work in Ant as it has its URLClassLoader set as
thread context ClassLoader. Now I searched around a bit about a correct way to
handle this and found two interesting things:


David Wood writes about a similar problem and solved it by using:

  // Get the task class loader we used to load this tag.
  AntClassLoader taskloader = (AntClassLoader)this.getClass().getClassLoader();

  // Shove it into the Thread, replacing the thread's ClassLoader:

Then you do what you have to do. When your done, I highly advise doing a:

  // Reset the Thread's original ClassLoader.

Is this a proof way of working around the problem?


With this commit for a scripting environment something similar is done. I only
wonder why this is not done for every classpath-related task. Wouldn't that be
more proof? And is there any reason that in ScriptRunner the
AntClassLoader.setThreadContextLoader() feature is not used, but setting the
thread context class loader is done by hand?


To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message