How to print Java object's current value in debug console in Eclipse?
Solution 1
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.
Solution 2
System.out.println(nameOfValue);
Solution 3
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.
Solution 4
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.
eonil
Favorite words: "Make it work, make it right, make it fast" — by Kent Beck? "...premature optimization is the root of all evil (or at least most of it) in programming." - from The Art of Computer Programming, by Donald Knuth. "Yes, but your program doesn't work. If mine doesn't have to work, I can make it run instantly and take up no memory." — from Code Complete, by Steve Mcconnell. "Flat is better than nested." — from The Zen of Python, by Tim Peters "Making decisions is slow." — from The Ninja build system manual, author unknown. "A little copying is better than a little dependency." - from Go Proverbs, by Rob Pike Preferred tools: macOS, iOS, Ubuntu. Rust, Swift, VIM, Xcode. SQLite, PostgreSQL, Redis. And a few more trivial stuffs. Feel free to fix my grammar. I always appreciate!
Updated on January 21, 2020Comments
-
eonil over 4 years
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?P.S. I'm debugging a Java app.
-
BangOperator almost 6 yearspo in Xcode console ~= Expression in most of the java IDE's
-
-
Baldrick over 12 years+1 . Also the "Expression" view (in the Debug perspective). You can put Java code in this view that is evaluated when you reach a breakpoint, and at every step in debug.
-
Perception over 12 years@Baldrick - totally forgot about the Expression view, which is another excellent tool.
-
eonil over 12 yearsI want command-line console because I don't want to use mouse when evaluating an expression because usually the expression for debugging goes complex. And also, aiming small icon with mouse is a lot harder than using keyboard.
-
eonil over 12 yearsI can't find the window that I can input this :(
-
James over 12 yearsIt should print it in the console or give you an error in the display window if the code could not be run
-
James over 12 yearsIs the object your trying to print a string, if so could the string be blank try another line after to print some text and see if there is a space between the last console output and the text you printed
-
Perception over 12 years@Eonil - ah! A keyboard guy. No I don't think you will find something exactly like that in Eclipse. Though I think once you try the other methods available you will find them very powerful. Best of luck!
-
eonil over 12 yearsThanks for suggestion. Actually I have been IDEs like VS or Eclipse for years, and finally I switched to keyboard based. Anyway it looks time to go back the old time haha :)
-
MichaelStoner about 8 yearsYou can put it in the expressions window
-
Steve Chambers over 7 yearsThe above code would go in the "Display" view since it is a command not an expression.