How to debug Tomcat source code breakpoint

Tomcat, as an old Web container framework, is widely used. Whether it is to learn the overall design of its framework or to better solve problems, we as programmers should have some understanding of Tomcat. The best way to understand a framework is to look at the official documentation, but sometimes the official documentation doesn’t answer our questions, and that’s where the source code comes in.

Whether using the compiler Idea or Eclipse, or with SpringBoot embedded with Tomcat, there is no way to break and debug how Tomcat is started.

Download the source code

What is the Tomcat source code? It’s not the JAR we downloaded, put the war package in the WebApp folder, and run start.shTomcat to start it. The source package is a separate package, as shown below. Download the source code

Configure POM files

If you download it and unzip it, you should have apache-tomcat-9.0.20-src, which contains the tomcat source code. Now we are going to run it, using Maven configuration. Configure the Pom file to download the JAR package required for Tomcat operation.

Create a new folder, such as tomcat9, and unzip the source code into the folder and resume the POM.xml file in tomcat9. The directory structure looks like this.

-- tomcat9 -- apache-tomcat-9.0.20-src -- pomCopy the code

The contents of the POM.xml file are as follows

<? 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/maven-v4_0_0.xsd"> < modelVersion > 4.0.0 < / modelVersion > < groupId > GXF < / groupId > < artifactId > apache tomcat - 9 < / artifactId > < name > apache tomcat - 9 - source < / name > < version > 1.0 < / version > < packaging > pom < / packaging > < modules > < module > apache tomcat - 9.0.20 - SRC < module > < / modules > < project >Copy the code

In this case, create a pom. XML file under apache-tomcat-9.0.20-src folder


-- tomcat9
	-- apache-tomcat-9.0.20-src
		-- pom.xml
	-- pom.xml

Copy the code

The pom.xml file contains the following contents

<? 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 >. Org. Apache tomcat < / groupId > < artifactId > Tomcat9.0 < / artifactId > <name>Tomcat9.0</name> <version>9.0</version> <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> The < version > 2.0.2 < / version > < configuration > < encoding > utf-8 < / encoding > <source> 1.8 < /source> <target>1.8</target> </configuration> </plugin> </plugins> </build> <dependencies> <groupId>ant</groupId> <artifactId>ant</artifactId> <version>1.7.0</version> </dependency> <groupId>ant</groupId> <artifactId>ant-apache-log4j</artifactId> <version>1.6.5</version> </dependency> <dependency> <groupId>ant</groupId> < artifactId > ant - Commons - logging < / artifactId > < version > 1.6.5 < / version > < / dependency > < the dependency > GroupId > wsdl4J </groupId> <artifactId>wsdl4j</artifactId> <version>1.6.2</version> </dependency> <dependency> < the groupId > javax.mail. XML. RPC < / groupId > < artifactId > javax.mail. XML. RPC - API < / artifactId > < version > 1.1 < / version > < / dependency > Piler < dependency > < groupId > org.eclipse.jdt.core.com < / groupId > < artifactId > ecj < / artifactId > < version > 4.6.1 < / version > </dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope>
        </dependency>
    </dependencies>
</project>

Copy the code

Setting Startup Parameters

After configuring the POM.xml file, you can configure the startup parameters and open the project with the compiler. I used Idea, click Configure in the upper right corner and add an Application

In this case, you need to set three parameters

  • Main class: Fixedorg.apache.catalina.startup.BootstrapWhich Tomcat startup class is used
  • VM options : -dcatalina. home=" War directory"For example, I created a home folder in the source directoryWork, webapps, logs, lib, confFolder. This is the working path of Tomcat
  • Use classpath of module: sets it to the module of the new folder

After the configuration is complete, directly start the line, at this time may report an error, because there are many of the source code we do not use the test class, we can all delete, and then start again.

Congratulations, you should have successfully started the Tomcat source code, through the Debug to find what you want in the source code.