如何以编程方式使用JDT编译器?

bur*_*mre 6 eclipse eclipse-plugin eclipse-jdt java-compiler-api

我使用JDT来编译我的java类.BatchCompiler返回一个字符串,但我需要一系列问题/错误及其列和行信息.compiler.compile(单位); 将错误打印到其printwriter,compiler.resolve(unit)正是我想要的,但它只能编译一个java文件.

我用这种方式创建了一个编译器对象:

Compiler compiler = new Compiler(env, DefaultErrorHandlingPolicies.exitAfterAllProblems(), new CompilerOptions(), requestor, new DefaultProblemFactory());
Run Code Online (Sandbox Code Playgroud)

并创建包含文件名和文件内容的CompilationUnits到编译器.

CompilationUnit[] units = project.toCompilationUnit();
Run Code Online (Sandbox Code Playgroud)

AFAIK,有两种编译方式,其中一种是返回void并将错误和问题打印到其PrintWriter的编译(单位)方法,因为它不返回对我没用的列信息.另一种方法是resolve(unit)方法,但它只能使用一个CompilationUnit.

compiler.resolve(units[index], true, true, true);
Run Code Online (Sandbox Code Playgroud)

有谁知道如何以编程方式使用JDT编译器来编译多个文件?

thS*_*oft 1

org.eclipse.jdt.internal.compiler.Compiler是内部API。根据其resolve方法的JavaDoc:Internal API used to resolve a given compilation unit. Can run a subset of the compilation process

相反,这里描述了编译 Java 文件和确定编译问题的官方方法。基本上,您创建一个 Java 项目并在其上调用 Eclipse 的构建器,然后查询该项目的 Java 问题标记。