通过Compiler API获取生成的文件的位置

时间:2019-05-25 10:57:11

标签: java compilation

如何获取由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();

0 个答案:

没有答案