try {
Class<?> main = newCL.loadClass("com.sun.tools.javac.Main");
Method method = main.getMethod("compile", String[].class, PrintWriter.class);
exitCode = (Integer) method.invoke(null, args.toArray(new String[args.size()]), writer);
} catch (ClassNotFoundException e1) {
throw new BuildException("Could not find javac compiler!", e1);
} catch (Exception e) {
throw new BuildException("Could not invoke javac compiler!", e);
}
// check exit code
if (exitCode != 0) {
writer.close();
System.out.println(out.toString());
throw new BuildException("Could not compile generated files! Code: " + exitCode);
}
}