/** Compile java src into java .class files */
private void compileJava(File tempDir) throws IOException, CompileException {
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
if (compiler == null) {
throw new CompileException("JDK required (running inside of JRE)");
}
DiagnosticCollector<JavaFileObject> diagnostics =
new DiagnosticCollector<JavaFileObject>();
StandardJavaFileManager fileManager =
compiler.getStandardFileManager(diagnostics, null, null);
try {
Iterable<? extends JavaFileObject> compilationUnits = fileManager
.getJavaFileObjectsFromFiles(getAllFiles(tempDir, ".java"));
ArrayList<String> options = new ArrayList<String>();
String classpath = getMyClasspath();
if (classpath != null) {
options.add("-classpath");
options.add(classpath);
}
options.add("-d");
options.add(tempDir.getPath());
JavaCompiler.CompilationTask task = compiler.getTask(
null,
fileManager,
diagnostics,
options,
null,
compilationUnits);
if (!task.call()) {
Locale myLocale = Locale.getDefault();
StringBuilder msg = new StringBuilder();
msg.append("Cannot compile to Java bytecode:");
for (Diagnostic<? extends JavaFileObject> err : diagnostics.getDiagnostics()) {
msg.append('\n');
msg.append(err.getKind());
msg.append(": ");
if (err.getSource() != null) {
msg.append(err.getSource().getName());
}
msg.append(':');
msg.append(err.getLineNumber());
msg.append(": ");
msg.append(err.getMessage(myLocale));
}
throw new CompileException(msg.toString());
}
} finally {
fileManager.close();
}
}