In the process of debugging the code, in order to better locate and solve the problem, we sometimes need to use remote debugging methods. In this article, let's take a look at how to use IntelliJ IDEA to debug remote Tomcat.
First, configure remote:
As shown in the above figure, click Edit Configurations to enter the following interface:
As shown in the above figure, we entered the Run/Debug Configurations interface, then click + in the upper left corner and select Remote:
As shown in the figure above, there are two categories: Mark 2 and Mark 3. For Mark 2,
Note 2: Transmission method, default is Socket;
For label 3,
Note 3: Debugging mode, default to Attach;
Then, copy the annotation 1, the command line parameters that IntelliJ IDEA automatically produces, and import it into the Tomcat configuration file. Taking the Linux system as an example, the import statement is:
The code copy is as follows:
export JAVA_OPTS='-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005'
If it is a Windows system, the import statement is:
The code copy is as follows:
set JAVA_OPTS=-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005
The difference between the two is the different keywords of the import statement and whether there are quotation marks. The import keywords of the Linux system are export and Windows is set; the import value of the Linux needs to be enclosed in single quotes, while Windows does not.
Next, modify the catalina.sh file in the bin directory of Tomcat (if it is a Windows system, modify the catalina.bat file), and add the above import statement to this file:
At this point, the configuration of IntelliJ IDEA remote debugging Tomcat has been completed, and the subsequent steps of debugging are done according to normal debugging techniques!
++++++ Off topic: I am a big Easter egg++++++
Here, we assume that the IP address of the server is 10.11.12.39 and the port number is 16203. After the setting is completed, we enter Debug mode. If the connection is successful, the following prompt will appear:
In addition, if we are debugging across multiple systems, we only need to configure Remote in the system we want to debug, set a breakpoint, start Debug mode, and then execute the program where the service starts to enter the breakpoint we set. Moreover, if we configure Remote locally and associate it with a Tomcat, in Debug mode, all functions involving the code where the breakpoint is located will enter the breakpoint we set.
For example, for Tomcat A on the server, multiple systems use this Tomcat, such as order subsystem, account subsystem, routing subsystem, etc., and multiple systems call each other. If the order subsystem adjusts the account subsystem, and the account subsystem adjusts the routing subsystem, then when we configure Remote in these three subsystems and query the account information of the merchant in the order subsystem, then we set a breakpoint on the account subsystem; in the account subsystem, we click to other underlying services to query the account balance of the merchant, etc.), and after starting the Debug mode, trigger the function of querying the merchant's account information in the order subsystem through unit tests or page operations, we will enter the breakpoint set in the above three subsystems in turn.
In addition, after we configure remote debugging, even if others start related services, we will enter our breakpoint and will be affected by the breakpoint we set. The service will continue to execute only after we execute the test. Finally, the remote debugging function is really powerful. Make good use of remote debugging and stay away from bugs!
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.