It seems to be impossible to use javax.tools.ToolProvider
from a custom classloader as required by Ant or Webstart: http://bugs.sun.com/view_bug.do?bug_id=6548428
javax.tools.ToolProvider.getSystemJavaCompiler()
loads javax.tools.JavaCompiler
into a URLClassLoader whose parent is the system classloader. The API does not seem to allow users to specify a parent classloader.
How can one use javax.tools.JavaCompiler
from a custom classloader?
For example:
- Ant loads
MyParserTask
MyParserTask
parses Java source-codeMyParserTask
is loaded byAntClassLoader
that delegates to the system classloaderjavax.tools.JavaCompiler
is loaded byURLClassLoader
thast delegates to the system classloader
At a later point, MyParserTask
invokes:
javax.tools.CompilationTask task = compiler.getTask(...);
com.sun.source.util.JavacTask javacTask = (com.sun.source.util.JavacTask) task;
javacTask.parse().next().accept(visitor, unused); // parsing happens here
- Seeing how the two classes reside on separate classloaders, there doesn't seem to be a way for
MyParserTask
to interact withJavacTask
without gettingClassCastException
errors.
Any ideas?