7

我使用 JDT 来编译我的 java 类。BatchCompiler 返回一个字符串,但我需要一组问题/错误及其列和行信息。编译器.编译(单位);将错误打印到它的打印机,compiler.resolve(unit) 完全符合我的要求,但它只能编译一个 java 文件。

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

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

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

CompilationUnit[] units = project.toCompilationUnit();

AFAIK,有 2 种编译方法,其中一种是 compile(units) 方法,它返回 void 并向其 PrintWriter 打印错误和问题,因为它不返回列信息,这对我没有用。另一种方法是 resolve(unit) 方法,但它只能与一个 CompilationUnit 一起使用。

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

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

4

1 回答 1

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 问题标记。

于 2013-10-03T08:44:08.663 回答