简体   繁体   中英

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

我知道这可能与 Ultimate Edition 有关,但是有没有办法在 Community Edition 中本地调试这些应用程序?

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

在此处输入图片说明

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)

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

@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. 在此处输入图片说明
    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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM