简体   繁体   中英

How to print Java object's current value in debug console in Eclipse?

I'm newbie on Eclipse.

In Objective-C, I could print value of an object in console window with this command.

po nameOfValue

Maybe gdb command. I'm using Eclipse now, what's the equivalent of this in Eclipse?

PS I'm debugging a Java app.

Eclipse has very robust debugging capabilities - much more so than Objective C.

First off, while debugging you can view the values of all variables in the Variables window. Additionally, in the lower part of the Variables window you can type arbitrary Java, select it, right click, then choose to Inspect or Execute. You can actually change the value of variables in your program this way, while its running.

You can do pretty much the same thing in your source pane. Highlight a variable, right click and choose to Inspect it. You can also type in a random expression and execute it. You can also places watches on variables (which I believe you can do in Objective-C), or on expressions.

There is an Expression view which is not displayed by default (on your menu select Window->Views->Expressions, while in Debug perspective). It allows you to add arbitrary (valid) Java expressions and the values of those expressions will then be watched over the lifetime of your debug session, very nifty. Thanks to @Baldrick for the reminder of this great tool.

System.out.println(nameOfValue);

I'm not aware of any printing option of the whole object state in console while debugging.

But you can override the toString() method of your object and there concatenate the string with the values of each field or whatever you want to print for that object. Then when calling somewhere in the code System.out.print(myObject); it will print the result of the toString() method which you've overridden.

Click Window menu select show view type display and show that view. In this view you can type java such as System.out.println(objectName); when the application has paused during debugging.

Override toString for that object/class. Then System.out.println(objectVariable).

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