远程调试Tomcat时IntelliJ IDEA断点无法触发问题
Hey there, let's dig into why your remote debugging isn't hitting those breakpoints. First, let's lay out all the details you provided, then break down the root cause and fixes:
Environment Setup
- OS: Mac 10.13.6
- Java: Java 8
- Tomcat: Tomcat 7
- IntelliJ: Community 2019.3
- Gradle: 5.2.1
Steps You Took
- Set Gradle debug options via terminal:
Quick note: There's a small typo here — theexport GRADLE_OPTS="-agentlib:jdwp=transport=dt_socket,server=y,suspend=address=5005"suspendparameter should besuspend=n(orsuspend=yif you want to wait for debugger attach before starting) followed byaddress=5005, like"-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005" - Ran the Tomcat task:
The output included:./gradlew myproj-web-java:tomcatRunWarListening for transport dt_socket at address: 5005
(Plus Gradle Daemon-related messages)
IntelliJ Debug Config
- Mode: Attach to remote JVM
- Module Classpath:
myproj-parent:myproj-web-java:main
What You Observed
- IntelliJ confirms the connection:
Connected to the target VM, address: 'localhost:5005', transport: 'socket'
- Browser loads the page normally, console logs appear, but breakpoints in your controller don't trigger
- When pausing the debugger, the main thread stack shows no business application code
- No separate Tomcat JVM process is visible in your process list
- The page remains accessible even when the debugger is paused
Why This Is Happening & How to Fix It
The key clues here are the missing Tomcat process and untriggered breakpoints — this means Tomcat is running inside the Gradle Daemon's JVM, not a separate process. When you attached to port 5005, you're debugging the Gradle Daemon itself, not the Tomcat instance hosting your web app.
Here's how to fix this:
- Configure Gradle to fork a separate JVM for Tomcat:
Open thebuild.gradlefile for yourmyproj-web-javamodule, and add this to the Tomcat configuration:tomcat { fork = true // Forces Tomcat to run in its own independent JVM jvmArgs = ["-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5006"] // Use suspend=y if you want Tomcat to wait for the debugger to attach before starting up } - Update your terminal and debug setup:
- Remove the
GRADLE_OPTSexport you set earlier (we're now defining debug args directly for Tomcat's dedicated JVM) - Run
./gradlew myproj-web-java:tomcatRunWaragain — you'll see Tomcat listening for debug connections on port 5006 now - Go to your IntelliJ remote debug config, change the port to 5006, and re-attach the debugger
- Remove the
- Double-check classpath alignment:
Make sure the module classpath in your debug config (myproj-parent:myproj-web-java:main) matches the runtime classpath of your Tomcat app. If IntelliJ feels out of sync with Gradle, runFile > Sync Project with Gradle Filesto ensure everything's aligned.
One last sanity check: Verify your breakpoints are set on code that's actually being executed. For example, place a breakpoint in the exact controller method that handles the browser request you're testing — breakpoints on unused code or interfaces won't trigger.
内容的提问来源于stack exchange,提问作者user1633272

