My use case is to collect the generated source files from a java program using the ToolProvider and JavaCompiler classes provided in JDK 6. The source files contain class references in the class loader (it works in the J2EE container), but not in the system class loader. I understand that by default ToolProvider will create an instance of JavaCompiler with a system class loader.
Is there a way to specify a class loader to use JavaCompiler?
I tried this approach, modified from IBM DeveloperWorks:
FileManagerImpl fm = new FileManagerImpl(compiler.getStandardFileManager(null, null, null););
with FileManagerImpl is defined as:
static final class FileManagerImpl extends ForwardingJavaFileManager<JavaFileManager> { public FileManagerImpl(JavaFileManager fileManager) { super(fileManager); } @Override public ClassLoader getClassLoader(JavaFileManager.Location location) { new Exception().printStackTrace(); return Thread.currentThread().getContextClassLoader(); } }
The column indicates that it is called only once during annotation processing. I checked the class specified in the source file to be compiled is not in the system class path, but is accessible from the class loader.
java classloader javac java-compiler-api
Phil
source share