AmirHd
AmirHd

Reputation: 10308

How to debug a Tomcat 7.x/8.x webapp locally with IntelliJ Community (free) version in Linux?

I understand that this is possible to do with the Ultimate Edition, but is there a way to debug these applications locally in the Community Edition?

Upvotes: 30

Views: 23252

Answers (4)

Nipun Thathsara
Nipun Thathsara

Reputation: 1139

@AmirHd's answer was very helpful. I'm using Idea Community version and you actually don't need to install any Tomcat plugins at all.

  1. Add below line at the top of your catalina.sh (in Linux) file which is located in the Tomcat bin directory.
JAVA_OPTS="$JAVA_OPTS -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5005"
  1. Go to Idea Run menu. Select Edit Configuration option.
  2. Click on the + sign and select Remote from the drop down.
  3. For that remote debugger, configure your Tomcat debug host and port. (Localhost, 5005) as below and save. enter image description here
    1. Start your Tomcat. (You will see it's in the debugging mode by the below log line.)
Listening for transport dt_socket at address: 5005
  1. Connect to that port using your Idea by pressing Alt + Shift + F9 or Run -> debug option.

No plugins at all.

Upvotes: 1

JanBrus
JanBrus

Reputation: 1483

For Windows users

put

set "JAVA_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5005"

line in catalina.bat then open command line in tomcat directory and run

catalina.bat run

Also note: I had to choose port 5005 because the default one in configuration settings in my IntelliJ was not editable.

Upvotes: 0

Amadeusz Iwanowski
Amadeusz Iwanowski

Reputation: 53

AmirHd answer is very helpful, but I had one more problem. When I changed JAVA_OPTS server wouldn't shutdown as in this post: Tomcat failed to shutdown I resolved this by changing

JAVA_OPTS

to

CATALINA_OPTS

Upvotes: 1

AmirHd
AmirHd

Reputation: 10308

History

If you have worked with IntelliJ Ultimate edition you have seen that it is possible to add a Tomcat configuration where you can both debug or run your Tomcat container from within IntelliJ. It is possible to get your IntelliJ Community version to do the same thing for you with a little bit of extra settings.

Relevant concepts

Tomcat JMX

This is a remote monitoring and management tool for Tomcat. JMX related settings is not needed to enable your debugging. Although it can be useful for monitoring purposes through JConsole (read more).

Different ports

You instance of Tomcat must be already up and running for the Community version to be able to attach itself to the Tomcat process. While your tomcat will be running on a port (Tomcat default is 8080), you also need to setup another port for the debugger to attach itself to your running version of Tomcat (9999 in our example).

Settings

Settings include changes you need to make to your Tomcat and configurations you need to do on your IntelliJ community edition.

Please follow the order in changes.

Tomcat side related changes

  1. Stop your Tomcat if it is running in your Tomcat bin folder through: ./shutdown or ./catalina stop

  2. Add the following line to your catalina.sh file under the commented JAVA_OPTS set statement:

    JAVA_OPTS="$JAVA_OPTS -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=9999"

HINT: I have used JAVA_OPTS not CATALINA_OPTS as the comments in catalina.sh suggests it is preferred to use JAVA_OPTS. However, it is possible to use CATALINA_OPTS in a similar manner to enable the debugging.

  1. Start Tomcat:

./catalina.sh start

IntelliJ related changes

enter image description here

Result

After click on debug button in your IntelliJ Community edition your debug section should open with the following line in its Console area:

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

Relevant posts and links

  1. Remote debugging with Tomcat (7) and Intellij (Very useful for Window only though)
  2. IntelliJ and Tomcat.. Howto..? (Misleading as it's old)
  3. IntelliJ ultimate and community version setups (Helpful but over complicated)
  4. IntelliJ IDEA 14.0.0 Web Help/Run/Debug Configuration: Tomcat (Somewhat useful)
  5. Debugging with Tomcat and Intellij Community Edition (Old and incomplete)

Upvotes: 33

Related Questions