javaeclipseclassjava-compiler-api

JavaCompiler - set the compiled class output folder


I'm using Eclipse and therefore my class files are stored in "bin" in the project folder. How can I set the JavaCompiler to output compiled classes into this "bin" folder?

My code:

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();

Solution

  • Need to pass your compiler options in compiler.getTask. The option is -d