I am new to java and I am following this tutorial as it is very informative and explains everything in great detail. The bottom of the tutorial explains how the JavaFileManager can be used to compile several java files and gives some examples of this, but I still can't get it to compile multiple files.
Another problem is that in this example it only ever shows how to compile one java file (which I can already work with), but these are several files that I had a problem with, since I want to be able to compile projects consisting from multiple java classes on my own system
This is what I have at the moment:
public static void main(String[] args) throws Exception { JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); // Line 1. MyDiagnosticListener listener = new MyDiagnosticListener(); // Line 2. StandardJavaFileManager fileManager = compiler.getStandardFileManager( listener, null, null); // Line 3. String fileToCompile = "test" + File.separator + "ManyErrors.java"; // Line 4 Iterable fileObjects = fileManager.getJavaFileObjectsFromStrings(Arrays .asList(fileToCompile)); // Line 5 CompilationTask task = compiler.getTask(null, fileManager, listener, null, null, fileObjects); // Line 6 Boolean result = task.call(); // Line 7 if (result == true) { System.out.println("Compilation has succeeded"); } } class MyDiagnosticListener implements DiagnosticListener { public void report(Diagnostic diagnostic) { System.out.println("Code->" + diagnostic.getCode()); System.out.println("Column Number->" + diagnostic.getColumnNumber()); System.out.println("End Position->" + diagnostic.getEndPosition()); System.out.println("Kind->" + diagnostic.getKind()); System.out.println("Line Number->" + diagnostic.getLineNumber()); System.out.println("Message->" + diagnostic.getMessage(Locale.ENGLISH)); System.out.println("Position->" + diagnostic.getPosition()); System.out.println("Source" + diagnostic.getSource()); System.out.println("Start Position->" + diagnostic.getStartPosition()); System.out.println("\n"); }
source share