如何使用JavaCompiler获取编译错误消息
这是我用来编译java类的代码:如何使用JavaCompiler获取编译错误消息,java,compiler-errors,javacompiler,Java,Compiler Errors,Javacompiler,这是我用来编译java类的代码: public void javaCompile(String fileName) throws IOException { JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>
public void javaCompile(String fileName) throws IOException {
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null);
Iterable<? extends JavaFileObject> compilationUnits = fileManager
.getJavaFileObjectsFromStrings(Arrays.asList(fileName));
JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, diagnostics, null,
null, compilationUnits);
boolean success = task.call();
fileManager.close();
System.out.println("Success: " + success);
}
public void javaCompile(字符串文件名)引发IOException{
JavaCompiler=ToolProvider.getSystemJavaCompiler();
DiagnosticCollector diagnostics=新建DiagnosticCollector();
StandardJavaFileManager fileManager=compiler.getStandardFileManager(诊断,null,null);
Iterable如果您只想在控制台中打印错误,请不要使用diagnosticsCollector,如下所示:
public void javaCompile(String fileName) throws IOException {
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
// DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
StandardJavaFileManager fileManager = compiler.getStandardFileManager(null/*diagnostics*/, null, null);
Iterable<? extends JavaFileObject> compilationUnits = fileManager
.getJavaFileObjectsFromStrings(Arrays.asList(fileName));
JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, null/*diagnostics*/, null,
null, compilationUnits);
boolean success = task.call();
fileManager.close();
System.out.println("Success: " + success);
}
public void javaCompile(字符串文件名)引发IOException{
JavaCompiler=ToolProvider.getSystemJavaCompiler();
//DiagnosticCollector diagnostics=新建DiagnosticCollector();
StandardJavaFileManager fileManager=compiler.getStandardFileManager(null/*diagnostics*/,null,null);
谢谢,这正是我所需要的。在这种情况下,你应该点击“勾号”来接受这个答案。
public void javaCompile(String fileName) throws IOException {
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
DiagnosticCollector<JavaFileObject> diagnosticsCollector = new DiagnosticCollector<JavaFileObject>();
StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnosticsCollector, null, null);
Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjectsFromStrings(Arrays.asList(fileName));
JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, diagnosticsCollector, null, null, compilationUnits);
boolean success = task.call();
if (!success) {
List<Diagnostic<? extends JavaFileObject>> diagnostics = diagnosticsCollector.getDiagnostics();
for (Diagnostic<? extends JavaFileObject> diagnostic : diagnostics) {
// read error dertails from the diagnostic object
System.out.println(diagnostic.getMessage(null));
}
}
fileManager.close();
System.out.println("Success: " + success);
}