简体   繁体   English

如何在 Linux 中使用 IntelliJ 社区(免费)版本在本地调试 Tomcat 7.x/8.x webapp?

[英]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.如果您使用过 IntelliJ Ultimate 版,您就会看到可以添加 Tomcat 配置,您可以在其中从 IntelliJ 中调试或运行您的 Tomcat 容器。 It is possible to get your IntelliJ Community version to do the same thing for you with a little bit of extra settings.通过一些额外的设置,可以让您的 IntelliJ 社区版本为您做同样的事情。

Relevant concepts相关概念

Tomcat JMX雄猫JMX

This is a remote monitoring and management tool for Tomcat.这是Tomcat的远程监控管理工具。 JMX related settings is not needed to enable your debugging.启用调试不需要 JMX 相关设置。 Although it can be useful for monitoring purposes through JConsole ( read more ).虽然它对于通过 JConsole 进行监控很有用( 阅读更多)。

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.您的 Tomcat 实例必须已经启动并运行,社区版本才能将其自身附加到 Tomcat 进程。 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).虽然您的 tomcat 将在一个端口上运行(Tomcat 默认为 8080),但您还需要为调试器设置另一个端口以将其自身附加到您正在运行的 Tomcat 版本(在我们的示例中为 9999)。

Settings设置

Settings include changes you need to make to your Tomcat and configurations you need to do on your IntelliJ community edition.设置包括您需要对 Tomcat 进行的更改以及您需要在 IntelliJ 社区版本上进行的配置。

Please follow the order in changes.请按照更改顺序。

Tomcat side related changes Tomcat端相关变化

  1. Stop your Tomcat if it is running in your Tomcat bin folder through: ./shutdown or ./catalina stop如果 Tomcat 在 Tomcat bin 文件夹中运行,请通过以下./catalina stop Tomcat: ./shutdown./catalina stop

  2. Add the following line to your catalina.sh file under the commented JAVA_OPTS set statement:将以下行添加到 catalina.sh 文件中注释的 JAVA_OPTS set 语句下:

    JAVA_OPTS="$JAVA_OPTS -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=9999" 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 .提示:我使用的是JAVA_OPTS而不是CATALINA_OPTS因为 catalina.sh 中的注释表明最好使用JAVA_OPTS However, it is possible to use CATALINA_OPTS in a similar manner to enable the debugging.但是,可以以类似的方式使用CATALINA_OPTS来启用调试。

  1. Start Tomcat:启动Tomcat:

./catalina.sh start ./catalina.sh 开始

IntelliJ related changes IntelliJ 相关变化

在此处输入图片说明

Result结果

After click on debug button in your IntelliJ Community edition your debug section should open with the following line in its Console area:单击 IntelliJ 社区版中的调试按钮后,您的调试部分应打开,并在其控制台区域中显示以下行:

Connected to the target VM, address: 'localhost:9999', transport: 'socket'连接目标VM,地址:'localhost:9999',传输:'socket'

Relevant posts and links相关帖子和链接

  1. Remote debugging with Tomcat (7) and Intellij (Very useful for Window only though) 使用 Tomcat (7) 和 Intellij 进行远程调试(虽然仅对 Window 非常有用)
  2. IntelliJ and Tomcat.. Howto..? IntelliJ 和 Tomcat ......如何......? (Misleading as it's old) (误导,因为它是旧的)
  3. IntelliJ ultimate and community version setups (Helpful but over complicated) IntelliJ 终极版和社区版设置(有用但过于复杂)
  4. IntelliJ IDEA 14.0.0 Web Help/Run/Debug Configuration: Tomcat (Somewhat useful) IntelliJ IDEA 14.0.0 Web 帮助/运行/调试配置:Tomcat (有点有用)
  5. Debugging with Tomcat and Intellij Community Edition (Old and incomplete) 使用 Tomcat 和 Intellij 社区版进行调试(旧的和不完整的)

AmirHd answer is very helpful, but I had one more problem. AmirHd 的回答很有帮助,但我还有一个问题。 When I changed JAVA_OPTS server wouldn't shutdown as in this post: Tomcat failed to shutdown I resolved this by changing当我更改 JAVA_OPTS 服务器时不会像这篇文章中那样关闭: Tomcat failed to shutdown我通过更改解决了这个问题

JAVA_OPTS

to

CATALINA_OPTS

@AmirHd's answer was very helpful. @AmirHd 的回答非常有帮助。 I'm using Idea Community version and you actually don't need to install any Tomcat plugins at all.我使用的是 Idea Community 版本,您实际上根本不需要安装任何 Tomcat 插件。

  1. Add below line at the top of your catalina.sh (in Linux) file which is located in the Tomcat bin directory.在位于 Tomcat bin 目录中的catalina.sh (在 Linux 中)文件的顶部添加以下行。
JAVA_OPTS="$JAVA_OPTS -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5005"
  1. Go to Idea Run menu.转到创意Run菜单。 Select Edit Configuration option.选择Edit Configuration选项。
  2. Click on the + sign and select Remote from the drop down.单击+号并从下拉列表中选择Remote
  3. For that remote debugger, configure your Tomcat debug host and port.对于该远程调试器,请配置 Tomcat 调试主机和端口。 (Localhost, 5005) as below and save. (Localhost, 5005) 如下所示并保存。 在此处输入图片说明
    1. Start your Tomcat.启动你的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.通过按Alt + Shift + F9Run -> debug选项,使用您的 Idea 连接到该端口。

No plugins at all.根本没有插件。

For Windows users对于 Windows 用户

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 中行然后在 tomcat 目录中打开命令行并运行

catalina.bat run

Also note: I had to choose port 5005 because the default one in configuration settings in my IntelliJ was not editable.另请注意:我必须选择端口 5005,因为 IntelliJ 中配置设置中的默认端口不可编辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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