Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use JDT compiler programmatically?

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?

like image 986
burak emre Avatar asked Jun 06 '13 19:06

burak emre


1 Answers

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.

like image 127
thSoft Avatar answered Sep 22 '22 15:09

thSoft