简体   繁体   中英

Debugging in Eclipse: Variable Snapshot Without Breakpoint

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 eg 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).

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 (eg 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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM