我在 Java 6 中的动态编译运行良好。但是,我想更改输出路径。我已经尝试了很多东西(我会饶过你)无济于事。无论如何,这是工作代码
String[] filesToCompile = { "testFiles/Something.java" };
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjects(filesToCompile);
CompilationTask task = compiler.getTask(null, fileManager, null,null, null, compilationUnits);
System.out.println("Good? " + task.call());
但是输出到源目录,这不是我想要的。
我怀疑答案可能在于,compiler.getTask
但 API 对于某些参数的含义并不是很明确。或者也许与文件管理器有关。我试过了
fileManager.setLocation(StandardLocation.locationFor("testFiles2"), null);
但同样,猜测可能不是一个好主意。
谢谢!
编辑:我也尝试过使用选项,就像这样(抱歉,如果有更紧凑的方式):
final List<String> optionsList = new ArrayList<String>();
optionsList.add("-d what");
Iterable<String> options = new Iterable<String>() {
public Iterator<String> iterator() {
return optionsList.iterator();
}
};
然后将选项传递给 getTask,但错误消息是“Invalid Flag”。