7

I use JDT to compile my java classes. BatchCompiler returns a string but I need an array of problems/errors with their column and row information. compiler.compile(units); prints the error to its printwriter, compiler.resolve(unit) does exactly what I want but it can compile only one java file.

I created a compiler object in this way:

Compiler compiler = new Compiler(env, DefaultErrorHandlingPolicies.exitAfterAllProblems(), new CompilerOptions(), requestor, new DefaultProblemFactory());

And create CompilationUnits that contains filenames and file contents to the compiler.

CompilationUnit[] units = project.toCompilationUnit();

AFAIK, there are 2 ways to compile, one of them is compile(units) method that returns void and prints errors and problems to its PrintWriter, because it doesn't return column information it's not useful for me. The other way is resolve(unit) method but it can work with only one CompilationUnit.

compiler.resolve(units[index], true, true, true);

Does anyone know how I can use JDT compiler programmatically to compile multiple files?

burak emre
  • 1,501
  • 4
  • 22
  • 46
  • Is there any problem in looping through the CompilationUnits and calling `resolve`. – Unni Kris Jun 11 '13 at 08:37
  • @UnniKris It works but as I said resolve takes only one CompilationUnit parameter. Looping though the CompilationUnits[] doesn't solve the problem because I'm trying to compile a project that includes many .java files depend on each other and the a CompilationUnit doesn't have any information about other CompilationUnits. – burak emre Jun 11 '13 at 23:50

1 Answers1

1

org.eclipse.jdt.internal.compiler.Compiler is internal API. According to the JavaDoc of its resolve method: Internal API used to resolve a given compilation unit. Can run a subset of the compilation process.

Instead, the official way of compiling Java files and determining the compilation problems is described here. Basically, you create a Java project and invoke Eclipse's builder on it, then query the project's Java problem markers.

thSoft
  • 21,755
  • 5
  • 88
  • 103