Tomcat8.5.49编译源码启动

前言

本人参考了一个博主,在此非常感谢,=====>https://www.cnblogs.com/zhiyouwu/p/11654442.html

虽然本人自己编译tomcat源码用的是8.5.49这个版本,但是我想更高的tomcat版本源码应该是操作大同小异的。

一、官网下载tomcat源码

重点:为什么想去了解tomcat源码,因为当了解tomcat启动过程做了什么,才能更深入去了解框架跟tomcat之间到底做了什么。

1.1、先下载tomcat源码
tomcat源码下载地址=====>https://tomcat.apache.org/download-80.cgi

windows版本

1.2、解压
把下载的tomcat源码包解压,然后得到apache-tomcat-8.5.49-src文件夹,然后点进去新建一个pom.xml

tomcat源码

pom.xml文件内容如下:

<?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打开解压出的文件夹

我这边所使用的的开发工具是idea。操作File->Open,然后找到你所解压出来的文件夹

image.png

2.1、启动tomcat工程
运行org.apache.catalina.startup包下的Bootstrap类的main方法,右键run就好了

错误1
出现了错误1:位于test包下面的TestCookieFilter里面许多类找不到。
解决方案:直接将这个类的内容注释掉,因为我们看源码并不需要用到这个类。

解决完错误1之后,配置Bootstrap的VM options选项
在VM options选项加上-Dcatalina.home="你所解压出来的源码文件夹位置"

image.png
配置完毕接下来我们再run一遍

错误2
出现了错误2: java.lang.ClassNotFoundException: listeners.ContextListener
解决方案:删除 webapps 下的 examples 文件夹!程序再次运行不报此错误!

删除examples
删除examples文件夹之后,接下来我们再run一遍

项目成功启动
接下来,我们去访问localhost:8080看看是否能进入首页

500错误
接下来我们看看后端

错误3

出现了错误3: java.lang.NullPointerException
解决方案:编辑 org.apache.catalina.startup.ContextConfig 文件的 configureStart() 方法,添加初始化 JSP 解析器的代码:

context.addServletContainerInitializer(new JasperInitializer(), null);
如下图:

ContextConfig类
接下来,我们再重新启动,再访问localhost:8080

成功

三、乱码问题

当你留意启动日志时候你会发现日志里面会输出乱码,这时候只需要配置Bootstrap的VM options选项。需要添加如下3个指示

-Duser.language=en
-Duser.region=US
-Dfile.encoding=UTF-8
解决乱码

最后重新启动就不会乱码了

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容