通過JSP編譯器編譯後生成了對應的java檔案,接下去要把Java檔案編譯成class檔案。對于這部分完全沒有必要重新造輪子,常見的優秀編譯工具有Eclipse JDT Java編譯器和Ant編譯器。Tomcat其實是同時支援兩個編譯器的,通過配置可以選擇,而預設是使用Eclipse JDT編譯器。
通過調用這些現成的編譯器的API就可以友善地實作對java檔案的編譯,由于兩個編譯器功能基本一樣,我們就挑預設編輯器看下它是如何進行編譯的,下面僅看如何用Eclipse JDT編譯器編譯java檔案。
Eclipse JDT提供了Compiler類用于編譯,它的構造函數比較複雜,如下所示,其實就是實作自定義構造函數包含的參數即基本完成了編譯工作。
public Compiler(
INameEnvironment environment,
IErrorHandlingPolicy policy,
CompilerOptions options,
final ICompilerRequestor requestor,
IProblemFactory problemFactory) {
}
為了說明友善直接上一個簡單的編譯實作,如下:
public class JDTCompile {
private static final File WORKDIR = new File("D:\\Program Files\\tomcat7\\work\\Catalina\\localhost\\test");
public static void main(String[] args) {
INameEnvironment nameEnvironment = new INameEnvironment() {
public NameEnvironmentAnswer findType(final char[][] compoundTypeName) {
return findType(join(compoundTypeName));
}
public NameEnvironmentAnswer findType(final char[] typeName, final char[][] packageName) {
return findType(join(packageName) + "." + new String(typeName));
private NameEnvironmentAnswer findType(final String name) {
File file = new File(WORKDIR, name.replace('.', '/') + ".java");
if (file.isFile()) {
return new NameEnvironmentAnswer(new CompilationUnit(file), null);
}
try {
InputStream input =
this.getClass().getClassLoader().getResourceAsStream(name.replace(".", "/") + ".class");
if (input != null) {
byte[] bytes = IOUtils.toByteArray(input);
if (bytes != null) {
ClassFileReader classFileReader = new ClassFileReader(bytes, name.toCharArray(), true);
return new NameEnvironmentAnswer(classFileReader, null);
}
}
} catch (ClassFormatException e) {
throw new RuntimeException(e);
} catch (IOException e) {
return null;
public boolean isPackage(char[][] parentPackageName, char[] packageName) {
String name = new String(packageName);
if (parentPackageName != null) {
name = join(parentPackageName) + "." + name;
File target = new File(WORKDIR, name.replace('.', '/'));
return !target.isFile();
public void cleanup() {}
};
ICompilerRequestor compilerRequestor = new ICompilerRequestor() {
public void acceptResult(CompilationResult result) {
if (result.hasErrors()) {
for (IProblem problem : result.getErrors()) {
String className = new String(problem.getOriginatingFileName()).replace("/", ".");
className = className.substring(0, className.length() - 5);
String message = problem.getMessage();
if (problem.getID() == IProblem.CannotImportPackage) {
message = problem.getArguments()[0] + " cannot be resolved";
throw new RuntimeException(className + ":" + message);
ClassFile[] clazzFiles = result.getClassFiles();
for (int i = 0; i < clazzFiles.length; i++) {
String clazzName = join(clazzFiles[i].getCompoundName());
File target = new File(WORKDIR, clazzName.replace(".", "/") + ".class");
try {
FileUtils.writeByteArrayToFile(target, clazzFiles[i].getBytes());
} catch (IOException e) {
throw new RuntimeException(e);
IProblemFactory problemFactory = new DefaultProblemFactory(Locale.ENGLISH);
IErrorHandlingPolicy policy = DefaultErrorHandlingPolicies.exitOnFirstError();
org.eclipse.jdt.internal.compiler.Compiler jdtCompiler =
new org.eclipse.jdt.internal.compiler.Compiler(nameEnvironment, policy, getCompilerOptions(),
compilerRequestor, problemFactory);
jdtCompiler
.compile(new ICompilationUnit[] {new CompilationUnit(new File(WORKDIR, "org\\apache\\jsp\\HelloWorld_jsp.java"))});
}
public static CompilerOptions getCompilerOptions() {
Map settings = new HashMap();
String javaVersion = CompilerOptions.VERSION_1_7;
settings.put(CompilerOptions.OPTION_Source, javaVersion);
settings.put(CompilerOptions.OPTION_Compliance, javaVersion);
return new CompilerOptions(settings);
private static class CompilationUnit implements ICompilationUnit {
private File file;
public CompilationUnit(File file) {
this.file = file;
}
public char[] getContents() {
try {
return FileUtils.readFileToString(file).toCharArray();
} catch (IOException e) {
throw new RuntimeException(e);
public char[] getMainTypeName() {
return file.getName().replace(".java", "").toCharArray();
public char[][] getPackageName() {
String fullPkgName = this.file.getParentFile().getAbsolutePath().replace(WORKDIR.getAbsolutePath(), "");
fullPkgName = fullPkgName.replace("/", ".").replace("\\", ".");
if (fullPkgName.startsWith("."))
fullPkgName = fullPkgName.substring(1);
String[] items = fullPkgName.split("[.]");
char[][] pkgName = new char[items.length][];
for (int i = 0; i < items.length; i++) {
pkgName[i] = items[i].toCharArray();
return pkgName;
public boolean ignoreOptionalProblems() {
return false;
public char[] getFileName() {
return this.file.getName().toCharArray();
private static String join(char[][] chars) {
StringBuilder sb = new StringBuilder();
for (char[] item : chars) {
if (sb.length() > 0) {
sb.append(".");
sb.append(item);
return sb.toString();
為了更好了解,我們根據構造函數的參數分别看看,
①INameEnvironment接口,主要需要實作的方法是findType和isPackage,findType是幫助JDT找到相應的java源檔案或者class位元組碼,根據傳進來的包名和類名去尋找。例如傳入“java.lang.String”或“org.apache.jsp.HelloWorld_jsp”則分别要找到JDK自帶的String位元組碼及tomcat中編譯的HelloWorld_jsp.java檔案。接着按要求封裝這些對象傳回JDT規定的NameEnvironmentAnswer對象。而isPackage則提供是否是包的判斷。
②IErrorHandlingPolicy接口,用于描述錯誤政策,可直接使用DefaultErrorHandlingPolicies.exitOnFirstError(),表示第一個錯誤就退出編譯。
③CompilerOptions對象,指定編譯時的一些參數,例如這裡指定編譯的Java版本為1.7。
④ICompilerRequestor接口,它隻有一個acceptResult方法,這個方法用于處理編譯後的結果,如果包含了錯誤資訊則抛異常,否則則把編譯成功的位元組碼寫到指定路徑的HelloWorld_jsp.class檔案中,即生成位元組碼。
⑤IProblemFactory接口,主要是控制編譯錯誤資訊的格式。
所有Compiler構造函數需要的參數對象都已經具備,傳入這些參數後建立一個Compiler對象,然後調用compile方法即可對指定的java檔案進行編譯。這裡完成了HelloWorld_jsp.java的編譯,結果生成了HelloWorld_jsp.class位元組碼。實際的tomcat中基本也是類似這樣使用JDT實作servlet的編譯,但它使用的某些政策可能不相同,例如使用DefaultErrorHandlingPolicies.proceedWithAllProblems()作為錯誤政策。
通過這兩章節“從JSP到Servlet”及“從Servlet到class位元組碼”,我們已經清楚tomcat對JSP編譯處理的整個過程了,先根據JSP文法解析生成類似xxxx.java的Servlet,然後再通過Eclipse JDT對xxxx.java編譯,最後生成了JVM能識别的class位元組碼。
<a target="_blank" href="https://item.jd.com/12185360.html">點選訂購作者《Tomcat核心設計剖析》</a>