Search code examples
javajavacjava-compiler-api

How to pass -X options programmatically to the java compiler


JavaCompiler - How to pass -X options programmatically to the JavaCompiler class?


Solution

  • The JavaCompiler page you linked to has some nice examples. They invoke the compiler with the following line of code:

    compiler.getTask(null, fileManager, null, null, null, compilationUnits1).call();
    

    The fourth argument to the getTask method is a list of option strings (really Iterable<String> but a list will be sufficient). So you can do:

    compiler.getTask(null, fileManager, null,
        Arrays.asList("-Xlint:all"),
        null, compilationUnits1).call();