How to enable remote debugging from IntelliJ with Tomcat?
How to enable remote debugging from IntelliJ with Tomcat?
TOMCAT Configuration Instructions
The process of getting remote debugging working involves two steps.
1. Starting Tomcat with remote debugging enabled
2. Having your IDE, in my case IntelliJ IDEA, to be able to debug the remote tomcat application.
There are couple of ways to get the first part done and it slightly differs depending on which OS environment your Tomcat instance is running on. But, regardless of the method used, the main idea behind the configuration remains the same; which is: pass specific start up options to the JVM that would enable remote debugging.
If you have Tomcat running as a windows service, then configuring Tomcat to start up with ability to be debugged remotely is done by simply specifying the start up arguments in the run properties.
Open up the Apache Tomcat properties dialog box :
Apache Tomcat/bin/tomcat9w.exe
and under the Java tab add the required start up option:
-agentlib:jdwp=transport=dt_socket,address=1043,server=y,suspend=n
Restart your server now.
Close and go to your IDE.
Configuring IntelliJ IDEA
With the remote JVM running the Tomcat started with the required start up arguments, the next thing to do is to configure the debugger in IntelliJ IDEA.
Open the Edit Configuration settings and select the Remote option:
The Remote settings dialog box appears where you can specify the required configuration; remote host, port, project, etc...
Specify the required settings, click Ok to save changes, and start the debugging session. You should also see the notice that IntelliJ has successfully connected to the remote VM.
Once this is done, you should then open the source code of the application you have running on the remote Tomcat, put a breakpoint where required and you can go ahead and start debugging as if the application is running on your local machine.
In Intellij Idea:
Click on Run -> Edit Configurations -> Add New Configurations ("+" icon)
Click on Remote
Set Host and Port
Copy content of Command line arguments for running remote JVM, for example:
-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:8000
If you use Tomcat Service (for Tomcat 9):
Go to %TOMCAT_INSTALL_DIR%/bin
Run tomcat9w.exe
Click on Java tab
In Java Options Paste the copied text:
-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:8000
Apply.
Step1 (How to start tomcat):
cd /C/SOFTWARE/apache-tomcat-8.5.38/bin
JAVA_OPTS='-agentlib:jdwp=transport=dt_socket,address=54470,suspend=n,server=y' sh catalina.sh start
ps aux | grep java
Step 2 ( Attach Intellij with remote port ):
Run/Debug Configurations
Tab: Server:
Application Server: Type "Tomcat 8.5.38"
Open browser -> URL -> http://localhost:8080/
Tomcat Server Settings -> Type -> Same File System
-> Host -> Same File System
Remote Connection Settings
-> Host -> localhost
-> Port -> 8080
Startup/Connection Tab
Run -> No specific parameters needed.
Debug -> -agentlib:jdwp=transport=dt_socket,address=54470,suspend=n,server=y