前言
本人參考了一個博主,在此非常感謝,=====>https://www.cnblogs.com/zhiyouwu/p/11654442.html
雖然本人自己編譯tomcat源碼用的是8.5.49這個版本,但是我想更高的tomcat版本源碼應(yīng)該是操作大同小異的。
一、官網(wǎng)下載tomcat源碼
重點(diǎn):為什么想去了解tomcat源碼,因?yàn)楫?dāng)了解tomcat啟動過程做了什么,才能更深入去了解框架跟tomcat之間到底做了什么。
1.1、先下載tomcat源碼
tomcat源碼下載地址=====>https://tomcat.apache.org/download-80.cgi

1.2、解壓
把下載的tomcat源碼包解壓,然后得到apache-tomcat-8.5.49-src文件夾,然后點(diǎn)進(jìn)去新建一個pom.xml

pom.xml文件內(nèi)容如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.github.sources</groupId>
<artifactId>source-tomcat</artifactId>
<version>9.0.26</version>
<name>source-tomcat</name>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.easymock</groupId>
<artifactId>easymock</artifactId>
<version>3.5.1</version>
</dependency>
<dependency>
<groupId>org.apache.ant</groupId>
<artifactId>ant</artifactId>
<version>1.10.1</version>
</dependency>
<dependency>
<groupId>wsdl4j</groupId>
<artifactId>wsdl4j</artifactId>
<version>1.6.2</version>
</dependency>
<dependency>
<groupId>javax.xml</groupId>
<artifactId>jaxrpc</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>org.eclipse.jdt</groupId>
<artifactId>org.eclipse.jdt.core</artifactId>
<version>3.18.0</version>
</dependency>
<dependency>
<groupId>org.eclipse.jdt.core.compiler</groupId>
<artifactId>ecj</artifactId>
<version>4.6.1</version>
</dependency>
</dependencies>
<build>
<finalName>Tomcat9.0</finalName>
<sourceDirectory>java</sourceDirectory>
<testSourceDirectory>test</testSourceDirectory>
<resources>
<resource>
<directory>java</directory>
</resource>
</resources>
<testResources>
<testResource>
<directory>test</directory>
</testResource>
</testResources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<encoding>UTF-8</encoding>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
二、用idea打開解壓出的文件夾
我這邊所使用的的開發(fā)工具是idea。操作File->Open,然后找到你所解壓出來的文件夾

2.1、啟動tomcat工程
運(yùn)行org.apache.catalina.startup包下的Bootstrap類的main方法,右鍵run就好了

解決方案:直接將這個類的內(nèi)容注釋掉,因?yàn)槲覀兛丛创a并不需要用到這個類。
解決完錯誤1之后,配置Bootstrap的VM options選項(xiàng)
在VM options選項(xiàng)加上-Dcatalina.home="你所解壓出來的源碼文件夾位置"


java.lang.ClassNotFoundException: listeners.ContextListener解決方案:刪除 webapps 下的 examples 文件夾!程序再次運(yùn)行不報此錯誤!




出現(xiàn)了錯誤3: java.lang.NullPointerException
解決方案:編輯 org.apache.catalina.startup.ContextConfig 文件的 configureStart() 方法,添加初始化 JSP 解析器的代碼:
context.addServletContainerInitializer(new JasperInitializer(), null);
如下圖:


三、亂碼問題
當(dāng)你留意啟動日志時候你會發(fā)現(xiàn)日志里面會輸出亂碼,這時候只需要配置Bootstrap的VM options選項(xiàng)。需要添加如下3個指示
-Duser.language=en
-Duser.region=US
-Dfile.encoding=UTF-8

最后重新啟動就不會亂碼了