I am new to java and I am following this tutorial as it is very informative and explains everything in great detail. At the bottom of the tutorial it explains how a JavaFileManager can be used to compile multiple java files and gives a few examples of this but i still cant get it to compile multiple files myself
Another problem is that in the example it only ever shows how to compile one java file (which i can already get working) but it is the multiple files that i am having a problem with as i want to be able to compile projects made up of multiple java classes in my own system
This is what i have to 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");
}