3

I am debugging a Java program in Eclipse. I would like to watch a specific variable. However, since my program uses a GUI, creating a breakpoint causes the window to freeze. This is particularly annoying when e.g. trying to right click on an item and navigate a context menu.

I don't actually want to stop the program, I just want to watch a specific variable and log its value every time a certain line is reached. At the moment, I am doing that with print statements, but I was wondering whether there was a way to do this using the debug view.

(Note: I don't want to write this to a log file. This is not a variable I need to look at long term. At the moment I just print it out, look at the values it takes on, and then delete the print statement. It would be nice to have something like this which I can keep in the debug view without having print / log statements cluttering my code).

3
  • 1
    i don't think its possible. without a breakpoint eclipse does not seem to allow to look into the values of variables Commented Feb 16, 2017 at 15:39
  • Can anyone suggest an alternative method? Commented Feb 17, 2017 at 8:02
  • the only thing you can do is printing the values at certain times Commented Feb 17, 2017 at 13:35

1 Answer 1

3

You can add a conditional breakpoint that returns false. The code in the condition will be evaluated, and then you return false to indicate to the debugger that you do not want to stop execution.

Right click on breakpoint properties, Check "Conditional" In the text field, enter the code you want to run (e.g. print x). Make sure the last statement in the condition is "return false"

Here is my code:

public static void main(String[] args) {
        String x  = " Some value"; 
        String y  = "a value"; //breakpoint here
        System.out.println("Hello World!");
    }

Say I want to print the value of x just after it is declared. Set a breakpoint where indicated in the code, Add a condition:

System.out.println("Value of x: " + x);
return false;

Output:

Value of x: Some Value
Hello World!
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.