如何获取由Compiler API生成的.class
文件的位置(路径)?即文件是由CompilationTask.call()
代码如下:
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>();
StandardJavaFileManager manager = compiler.getStandardFileManager(diagnostics, null, null);
File file = new File(System.getProperty("user.dir") + "/target/classes/tmp/" +"FileName.java");
Iterable<String> options = Arrays
.asList(new String[] { "-d", System.getProperty("user.dir") + "/target/classes/" });
Iterable<? extends JavaFileObject> sources = manager.getJavaFileObjectsFromFiles(Arrays.asList(file));
CompilationTask task = compiler.getTask(null, manager, diagnostics, options, null, sources);
task.call();
manager.close();