Gangmax Blog

Tomcat Debugging with IntelliJ IDEA Community Edition

In Eclipse EE edition, there is support to debugging web application with web container implementation like Tomcat. IntelliJ IDEA professional edtion seems have the same function. However, in IntelliJ IDEA community edition, how to do it?

This solution is found here.

First, update your Tomcat starting script file “bin/catalina.sh”, add the following content right after the “JAVA_OPTS” comment:

1
2
3
# From http://stackoverflow.com/questions/27655978/how-to-debug-a-tomcat-7-x-8-x-webapp-locally-with-intellij-community-free-vers
# to enable IntelliJ IDEA Community Edition Debugging Feature with Tomcat.
JAVA_OPTS="$JAVA_OPTS -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=9999"

Note the “address=9999” means the network port number used by Tomcat for debugging.

Second, open IntelliJ IDEA community edition, go to “Run -> Edit Configurations”, click the “+” icon and select “Remote” from the list, then you will see a dialog like below:

Add Debug Configuration

Note that you only need to update the “Port:” field and the “Search sources using module’s classpath:” list, the “Command line arguments for running remote JVM” will be changed accordingly as you update the “Port” field and does not need manually update.

In the “Port:” field, just write “9999” as you set in the “bin/catalina.sh” file. In the “Search sources using module’s classpath:” list, select you project which will running in Tomcat. And remember to give a name like “Tomcat” for this “run/debug configuration” item.

Third, configure Tomcat to use the project built files as the web application context. To do so, you need to create a “${TOMCAT_HOME}/conf/Catalina/localhost/ROOT.xml” file under the Tomcat installation directory:

${TOMCAT_HOME}/conf/Catalina/localhost/ROOT.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<!--
Note that:
1. Assume your project directory is "/home/user/code/hello-world/";
2. The ROOT "/" context path is used.
-->

<Context
docBase="/home/user/code/hello-world/target/ROOT"
path=""
reloadable="true"
/>

<!--
From:
http://stackoverflow.com/questions/7276989/howto-set-the-context-path-of-a-web-application-in-tomcat-7-0
https://tomcat.apache.org/tomcat-8.0-doc/config/context.html
-->

Now, you just need to start Tomcat as usual(run “./bin/startup.sh”), then in IntelliJ IDEA, open “Run -> Debug ‘Tomcat’”. If everything works, you will see the following output in the “Console” tab in the “Debug” tool window:

Connected to the target VM, address: ‘localhost:9999’, transport: ‘socket’

Now you can add break point in your project source code, and access your web application hosted on Tomcat to debug.

Enjoy!

Comments