JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
if (compiler == null) {
throw new RuntimeException(
"Cannot get the System Java Compiler. Please use a JDK, not a JRE.");
}
StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
ArrayList<File> files = new ArrayList<File>();
for (File f : new FileIterator(jspClassDir)) {
if (f.getPath().toLowerCase().endsWith(".java")) {
files.add(f);
}
}
if (files.isEmpty()) {
return;
}
List<String> optionList = new ArrayList<String>();
optionList.addAll(Arrays.asList("-classpath", classpath.toString()));
optionList.addAll(Arrays.asList("-d", jspClassDir.getPath()));
optionList.addAll(Arrays.asList("-encoding", opts.getCompileEncoding()));
Iterable<? extends JavaFileObject> compilationUnits =
fileManager.getJavaFileObjectsFromFiles(files);
boolean success = compiler.getTask(
null, fileManager, null, optionList, null, compilationUnits).call();
fileManager.close();
if (!success) {
throw new JspCompilationException("Failed to compile the generated JSP java files.",
JspCompilationException.Source.JSPC);
}