Java编译器-设置编译后的类输出文件夹

5
我正在使用Eclipse,因此我的类文件存储在项目文件夹中的“bin”中。我该如何设置Java编译器以将编译后的类输出到这个“bin”文件夹?
我的代码:
File fRun = new File("FileToCompile");
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
Iterable<? extends JavaFileObject> compUnits =  fileManager.getJavaFileObjects(fRun);
Boolean compRes = compiler.getTask(null, fileManager, null, null, null, compUnits).call();          

if(compRes == true){
    System.out.println("Compilation has succeeded");
    ClassLoader cl = Thread.currentThread().getContextClassLoader();
    Class<?> compiledClass = cl.loadClass("data.testcases.TestA");
    cRun = compiledClass;
}else{
    System.out.println("Compilation error");
    fileManager.close();

需要在compiler.getTask中传递编译器选项。选项是“-d”。 - Chetter Hummin
这不应该成为答案而不是评论吗,@AmitBhargava? - nwinkler
呵,我觉得它不够详细,所以就让它成为了一条注释。我也会添加一个答案。 - Chetter Hummin
1个回答

11

需要在compiler.getTask中传递编译器选项。该选项为-d。


我应该在哪里添加编译器选项? - Arian
1
这将是第四个参数。现在,我还没有尝试过,但以下代码片段应该有所帮助:final Iterable<String> options = Arrays.asList( new String[] { "-d", <path>} ); 其中<path>是您bin目录的绝对路径。 - Chetter Hummin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接