Using Eclipse’s Java Debugger: Breakpoints, Inspecting Variables, and Expression Evaluation
Eclipse’s Java debugger lets you pause execution, inspect variables, and evaluate expressions on the fly. This guide walks through setting breakpoints, starting a debug session, and common pitfalls to avoid, with a concrete code example.
27 Nov 2025, 18:07 UTC

Why Use Eclipse’s Java Debugger
When a Java program doesn’t behave as expected, stepping through the code line‑by‑line reveals the exact state that leads to the problem. Eclipse’s integrated debugger gives you that visibility without leaving the IDE, letting you pause execution, examine local and field values, and evaluate arbitrary Java expressions on the fly.
Setting a Breakpoint
Breakpoints are the pause points that tell the debugger where to stop. In Eclipse you can set a line breakpoint in two ways:
- Double‑click the left margin (the gutter) next to the source line you want to stop at.
- Open the Breakpoints view (Window > Show View > Breakpoints) and click Add Java Line Breakpoint to specify a class, method, and line number.
For example, consider a project named DebugDemo with the following class:
public class DebugDemo {
public static void main(String[] args) {
int result = compute(5);
System.out.println("Result: " + result);
}
private static int compute(int x) {
int y = x * 2; // Breakpoint here
return y + 3;
}
}
Double‑click the line marked int y = x * 2; to place a breakpoint. Eclipse will show a red dot in the gutter and record the breakpoint in the Breakpoints view.
Starting a Debug Session
To launch the program under the debugger, choose Run > Debug As > Java Application. If the project is configured correctly, Eclipse will:
- Compile the source (make sure Build automatically is checked in Project > Build Automatically).
- Start the Java Virtual Machine (JVM) with debugging enabled.
- Open the Debug perspective automatically, displaying views such as Debug, Variables, and Display.
When the JVM reaches the breakpoint, execution halts. The Debug view shows the current call stack; the Variables view lists local variables for the active frame.
Inspecting Variables
In the Variables view you can:
- Expand nested objects to see their fields.
- Right‑click a variable and choose Display to view it in the Display view.
- Use the Pin icon to keep a variable visible while stepping.
For the example above, when paused at the breakpoint, the Variables view will show:
args = ["5"]
x = 5
Expanding args reveals the array contents.
Evaluating Expressions in the Display View
The Display view lets you type any valid Java expression that is accessible in the current stack frame. After typing the expression, press Ctrl+R (or click Run) to evaluate it:
// While paused at compute()
int y = x * 2;
// In Display view
x + y + 10
Pressing Run will replace the expression with its result, e.g., 20, and keep the cursor on the expression for quick re‑evaluation.
Common Mistakes and Limits
- Missing Debug Information: If the compiler is run with
-g:noneor a custom build tool strips line numbers, breakpoints will never be hit. Ensure the project’s Java Compiler settings include-g(default in Eclipse). - Wrong Launch Configuration: Running a launch that points to a different main class or uses a different classpath will start a JVM that does not contain the compiled classes, so the debugger cannot attach. Verify the launch configuration in Run Configurations.
- Dead Code: Setting a breakpoint on a line that is never executed (e.g., inside a
if (false)block) will never pause. Use the Coverage view or Analyze tools to confirm execution paths. - External Libraries: Breakpoints in JARs that are not on the project’s classpath or are loaded by a separate classloader will be ignored unless you add the JAR to the Debug view’s Breakpoints list.
- Stepping Over Optimized Code: In highly optimized builds, the compiler may inline methods or remove variables, making the debugger’s view misleading. Use a non‑optimized build for debugging.
To check that the debugger is working, after pausing at a breakpoint, press F6 (Step Into) or F5 (Step Over). The cursor should move to the next line, and the Variables view should update accordingly. If the program continues without stopping, re‑inspect the build settings and launch configuration.
Practical Checklist
- Enable Build Automatically (Project > Build Automatically).
- Set a breakpoint on a line that executes.
- Run the project via Debug As.
- Confirm the Debug perspective opens.
- Verify the Variables view shows expected values.
- Evaluate an expression in the Display view.
- Use F5 and F6 to step through code.
- Check the Call Stack to verify the correct frame.
- When finished, switch back to the Java perspective.
By following this workflow, you’ll quickly pinpoint logical errors, confirm variable states, and gain confidence that your Java application behaves as intended.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.