Search code examples
javaantclassloader

Default Classloader when executing ANT task


I have implemented a custom ANT task (extending org.apache.tools.ant.Task) which uses internally the context classloader from the current thread. (looks like this)

Thread.currentThread().getContextClassLoader()

If the task is executed for example via a random main method from any java class everything just works as expected. The problem is that if I build an jar, containing this task and everything it needs and define in anothers project build.xml a for my own task... then ANT seems to use its own classloader. This results in many problems, since the ANT classloader can´t know my classes.

Is there any workaround for this? Doing things like the following has not helped: (inside execute method of my task)

 Thread.currentThread()
       .setContextClassLoader(AnyOwnClass.class.getClassLoader());

Is it even possible to get the "real" classloader? Any help would be nice


Solution

  • See this question for details, one possible solution may be the ant-classloader task.