Master Java Debugging: From IDEA Tricks to JPDA Deep Dive
This article explains why debugging is a core skill for developers, walks through essential IntelliJ IDEA debugging features, introduces command‑line debugging with jdb, and demystifies remote debugging by exploring the JPDA architecture and its underlying protocols.
Debugging, literally "debug", is an essential skill in software development; mastering it allows developers to quickly locate and fix bugs in code.
Whether using IntelliJ IDEA or Eclipse, the debugger is a standard tool. Proper use of breakpoints, stepping, and evaluation expressions can dramatically speed up problem diagnosis, while relying on System.out.println for debugging is discouraged.
1. Practical IDEA Debugging Techniques
IntelliJ IDEA provides a debugging panel divided into five sections: step tracing, breakpoint management, expression evaluation, stack and thread views, and variable watching.
Step tracing
Breakpoint management
Expression evaluation
Stack and thread inspection
Variable observation
1.1 Step Tracing
Common shortcuts include Show Execution Point to jump to the current line, Step Over to execute a line without entering called methods, Step In / Force Step In to dive into methods (including JDK methods with Force Step In), Step Out to finish the current method, Drop to Frame to return to a previous stack frame, and Run to Cursor / Force Run to Cursor for temporary breakpoints.
1.2 Breakpoint Management
Breakpoints can be line breakpoints or global breakpoints. Line breakpoints pause execution at a specific line, while global breakpoints pause when a condition is met. IDEA allows configuring suspend mode (All/Thread), conditions, log messages, evaluate‑and‑log, removal after hit, instance/class filters, and pass count.
Example of a conditional breakpoint that stops when person.name = 'Zhangsan':
stop in Class.Method1.3 Expression Evaluation
The "Evaluate Expression" tool lets you view variable values or compute arbitrary expressions. It offers two modes: Expression Mode and Code Fragment Mode, similar to Eclipse's Expression and Display views.
1.4 Stack and Thread Views
The Stack view shows the call stack and allows switching frames; the Thread view lists all threads and supports thread dumps.
1.5 Variable Observation
Variables can be observed in separate panels, allowing real‑time monitoring and even modification of values during debugging.
2. Command‑Line Debugging with jdb
jdb is the Java command‑line debugger bundled with the JDK. Start debugging with jdb Test, set breakpoints using stop in Test.main or stop at Test:25, and control execution with run, step, print, locals, and cont. Use list to view source around the current line.
> stop in Test.main
Running...
> run
VM started...Specify the source path when class files are separate from source files:
# jdb -sourcepath path/to/source Test3. Remote Debugging
Remote debugging uses the JDWP agent. The JVM is started with
-agentlib:jdwp=transport=dt_socket,address=127.0.0.1:5005,suspend=y,server=n(or shared‑memory on Windows). The debugger connects via a socket or shared memory connector. IDEA configures a remote debug configuration that launches the JVM with these parameters.
"C:\Program Files\Java\jdk1.8.0_111\bin\java" -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:20060,suspend=y,server=n FooConnectors include Socket‑attaching, Shared‑memory attaching, Socket‑listening, Shared‑memory listening, and Command‑line launching. Transport can be socket or shared memory.
4. Java Debugging Architecture (JPDA)
JPDA consists of three layers: JVMTI (native VM interface), JDWP (debugger‑to‑VM protocol), and JDI (high‑level Java API). JDWP transports commands between the debugger and the VM, while JVMTI provides the actual debugging capabilities.
Understanding JPDA helps explain how IDEA, jdb, and other tools interact with the JVM.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
