5

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();
3
  • Need to pass your compiler options in compiler.getTask. The option is -d Commented Mar 12, 2012 at 11:00
  • Shouldn't this be the answer instead of a comment, @AmitBhargava? Commented Mar 12, 2012 at 11:02
  • Heh. Thought it wasn't detailed enough, so I let it be a comment. I'll add an answer too. Commented Mar 12, 2012 at 11:04

1 Answer 1

13

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

Sign up to request clarification or add additional context in comments.

2 Comments

and where do I add compiler options?
This would be the fourth parameter. Now, I haven't tried this out but the following code fragment should help : final Iterable<String> options = Arrays.asList( new String[] { "-d", <path>} ); where <path> is the absolute path for your bin directory

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.