DiagnosticCollectorCompile.java
01 | import java.io.IOException; |
02 | import java.util.Arrays; |
03 |
04 | import javax.tools.DiagnosticCollector; |
05 | import javax.tools.JavaCompiler; |
06 | import javax.tools.JavaFileObject; |
07 | import javax.tools.StandardJavaFileManager; |
08 | import javax.tools.ToolProvider; |
09 |
10 | public class DiagnosticCollectorCompile { |
11 |
12 | public static void main(String args[]) throws IOException { |
13 | JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); |
14 | DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>(); |
15 | StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null , null ); |
16 | Iterable<? extends JavaFileObject> compilationUnits = fileManager |
17 | .getJavaFileObjectsFromStrings(Arrays.asList( "Foo.java" )); |
18 | JavaCompiler.CompilationTask task = compiler.getTask( null , fileManager, diagnostics, null , |
19 | null , compilationUnits); |
20 | boolean success = task.call(); |
21 | fileManager.close(); |
22 | System.out.println( "Success: " + success); |
23 | } |
24 | } |