How can I use JDT compiler programmatically?
Asked Answered
C

1

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?

Cupulate answered 6/6, 2013 at 19:57 Comment(2)
Is there any problem in looping through the CompilationUnits and calling resolve.Antoineantoinetta
@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.Cupulate
P
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.

Philia answered 3/10, 2013 at 8:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.