131

I want to know how to place a conditional breakpoint in Eclipse. I have a code like:

public static void doForAllTabs(String[] tablist){
    for(int i = 0; i<tablist.length;i++){
-->        doIt(tablist[i]);
    }
}

Now I want to put a breakpoint on the line with the arrow but want it to trigger only if:

tablist[i].equalsIgnoreCase("LEADDELEGATES");

6 Answers 6

197

Put your breakpoint. Right-click the breakpoint image on the margin and choose Breakpoint Properties:

enter image description here

Configure condition as you see fit:

enter image description here

5
  • 9
    The latest Eclipse includes these options directly in the Breakpoints View as well. Commented Aug 25, 2011 at 17:58
  • 8
    Wow! I've been using: if(condition){System.out.println("debug");} and breaking on the print statement for years.
    – Cruncher
    Commented Apr 16, 2014 at 19:13
  • 6
    Don't forget to OMIT the ; at the end - I got an error when I included it.
    – modulitos
    Commented Sep 23, 2014 at 4:12
  • someString.equals("match-this-text") Commented Sep 19, 2019 at 19:50
  • For some reason still stops on it, despite I set the condition :(
    – RAM237
    Commented Dec 2, 2019 at 10:29
12

Make a normal breakpoint on the doIt(tablist[i]); line

Right-click -> Properties

Check 'Conditional'

Enter tablist[i].equalsIgnoreCase("LEADDELEGATES")

2
  • Don't you need a semicolon at the end of the expression? I can only get conditional breakpoints to work if I add the semicolon (and not very often even then).
    – Cajunluke
    Commented Aug 25, 2011 at 17:06
  • 3
    @CajunLuke Same here. I just tried it with a final T[] (non-generic type) and get Conditional breakpoint has compilation error(s). Amusingly, my condition is bonds==null and the Reason in the error dialog is invalid AssignmentOperator. sigh Sometimes Eclipse behaves more like a 12 year old human than 12 year old software.
    – KomodoDave
    Commented Apr 3, 2013 at 21:47
5

1. Create a class

public class Test {

 public static void main(String[] args) {
    // TODO Auto-generated method stub
     String s[] = {"app","amm","abb","akk","all"};
     doForAllTabs(s);

 }
 public static void doForAllTabs(String[] tablist){
     for(int i = 0; i<tablist.length;i++){
         System.out.println(tablist[i]);
    }
  }
}

2. Right click on left side of System.out.println(tablist[i]); in Eclipse --> select Toggle Breakpoint

3. Right click on toggle point --> select Breakpoint properties

4. Check the Conditional Check Box --> write tablist[i].equalsIgnoreCase("amm") in text field --> Click on OK

5. Right click on class --> Debug As --> Java Application

3

From Eclipsepedia on how to set a conditional breakpoint:

First, set a breakpoint at a given location. Then, use the context menu on the breakpoint in the left editor margin or in the Breakpoints view in the Debug perspective, and select the breakpoint’s properties. In the dialog box, check Enable Condition, and enter an arbitrary Java condition, such as list.size()==0. Now, each time the breakpoint is reached, the expression is evaluated in the context of the breakpoint execution, and the breakpoint is either ignored or honored, depending on the outcome of the expression.

Conditions can also be expressed in terms of other breakpoint attributes, such as hit count.

1
  • Your link's not got the ? encoded. Try this.
    – KomodoDave
    Commented Apr 3, 2013 at 21:44
0

There are Simple step which you need to follow to trigger the conditional breakpoint.

Step 1- Put the breakpoint on that line which you want to be run when the breakpoint condition falls True (Now the question arises where to put the breakpoint condition)

Step 2- After putting the breakpoint simply right click on the small breakpoint image placed on the left margin of your code.

Step 3- After clicking you will find an some options click on the breakpoint properties.

Step 4- After clicking you will find checkbox i.e Conditions, simply check it

Step 5- There will be text box Enable in which you need to write the condition upon which the line which has breakpoint will have been executed only when the condition falls true

Hope you find my Answer usefull

0

A way that might be more convenient: where you want a breakpoint, write a if statement and set a breakpoint in its contents.

if(tablist[i].equalsIgnoreCase("LEADDELEGATES")) {
-->    int breakpoint = 0; //don't do anything
}

(the breakpoint is represented by the arrow)

This way, the breakpoint only triggers if your condition is true. This could potentially be easier without that many pop-ups.

1
  • would be interesting to know whether this is the way conditional breakpoints are implemented under the hood or whether java has a special facility for them
    – timwaagh
    Commented Mar 18, 2021 at 11:08

Not the answer you're looking for? Browse other questions tagged or ask your own question.