StringBuffer variable value not coming in full in eclipse debug [duplicate] - java

While debugging Java code, Strings in the views "Variables" and "Expressions" show up only till a certain length, after which Eclipse shows "..."
Is there any way to inspect the entire string? (This eases the pain of adding logging statements for debugging everywhere)

In the Variables view you can right click on Details pane (the section where the string content is displayed) and select "Max Length..." popup menu. The same length applies to expression inspector popup and few other places.

In the Variables view you right-click on the variable and select Change value. If your variable is huge you have to wait a few minutes (in this case Eclipse doesn't respond to commands) but in the end Eclipse will show your variable entirely.

If you have a really long string, I use a different strategy: dump it to a file. I have a snippet of code I use in the Display view, which is evaluated to dump the data to a file. I use Commons IO, but you can do it with JDK only methods.
org.apache.commons.io.FileUtils.writeStringToFile(new java.io.File("<filename>"), <expression to evaluate>);
Naturally, you will need the Commons IO JAR in your classpath for that to work. If you don't, resort to JDK work.

The best way to view a String value from eclipse debug view is as below.
1) Switch to debug view in Eclipse
2) Select the desired variable in variable pane.
3) Right click on display area of variable pane where value is shown and click on Max Length. Enter the maximum char value in Configure Details Pane .
4) Cheers

When debugger reaches the point where you want the String value, just type a sysOut statement
System.out.println("The value is : \n " + query);
Select the above the statement, right click-> Execute
It will print the value on the console

For javascript based debugging on eclipse, "change value" method and "Max length" method both failed for me, adding the required object to watch(Expressions) and then Right Clicking the watched expression to select "Load Full Value" is the only solution that works for me, but even this inserts unwanted "\n" in the output.
Note - "Max length" must be set for the "Load Full Value" to work as it loads values till max length(default in eclipse is 10000). Refer above answer to see how to set Max length.

There is no "maxLength" in Eclipse Mars. And, the "change value" only works with "Variables", not "Expressions", so none of above works for me.
And, the expression are cut in the middle, not at the end. I guess they changed the default behaviour.
The only thing working for me, is to expand the expression name column's width, and click the expression to select it all to see the full length content.

Nothing from the above worked for me, moreover, some user interface elements in my Eclipse can not be found as described. I'm using STS 4.3.1.
The value I needed was really big, it is part of a huge JSON request. Workaround is to use an expression in Eclipse Debug Shell console and to output the substring of the whole value. Since you can see the partial value, inspect it and use the last few literals as the position to output the next chunk of the string. Repeat this approach until get what you need:
String result = new String(reallyBigByteArrayValue, "UTF-8");
result.substring(result.indexOf("some-unique-text"));

In Eclipse IDE 2020-09 it's "Pretty print to console" on right click.
From the console the copying is possible at full length.

Related

Eclipse debug mode view instance variable values

I am trying to view the instance variables values of the Arraylist in eclipse in debug mode but apart from the values in the list am unable to view variables like elementData or modCount that are part of the arraylist. I have tried using Watch, Display option as well but to no avail. Below is my screenshot after setting the breakpoint and triggering the program run:
Below is the screenshot that I found in one of the websites where the instance variables like elementData,modCount and size are visible:
In the current version (Oxygen.3) the default formatter for Collection shows only the members of the collection as an array. I'm not sure when the more detailed formatter was removed, but you can easily create a logical structure formatter.
Right click on the variable in the Variables tab and select Show Logical Structure/Edit Logical Structure
Your view will have the Collection entry hilighted, and won't contain the ArrayList entry I just created.
Add a new entry:
Provide the class name you want to format, and a description. Select List of Variables and then add the variables you want to display, providing a code snippet for each one. The code snippet executes as a method of the class.
End result:
In oxygen, "Show Logical Structure" is enabled by default. All you have to do is click on it to disable it.
[Image taken from eclipse.org]
In some instances that I cannot view variables, like in your case, I would suggest using Expression tab and add new expression. To load expression tab on Eclipse IDE, click on Window > Show View > Expression. Set breakpoint, hit debug, then expression tab should load arrNames and you can expand to view properties and data.
Go to Window->show view->expressions then in Expressions window go to variables tab
there you will see your variables right click on any one of them you will see Show logical structure.
Untick the Array option from there and debug again you will see elementData,modCount and size varibles.click here for image

Java Eclipse evaluate expression

Is it possible to evaluate a expression in Eclipse similar to IntelliJ. Where you can dynamically type code and the result will be displayed during debug?. I know it does something similar "Display" but you need to highlight code that has been written (so you cant write any new code unless you re-compile).
Perhaps there are plugins that i could use?. I have just started using eclipse
There is a Display view as well that can do exactly what you want. Go to Window > Show view > Display (or Other... if the Display view is not there. In the Display view, you can type any code you want during debug (content assist is available). The current objects and variables are also available to use. After you wrote your code, you have to highlight it, right click and select Execute or hit CTRL-U. You may play around with the other possible actions as well.
Besides that, you can also write in the source code during debug and evaluate it using Display as you mentioned, if you have the source for the class you are debugging.
1 - Type your expression inside a method that you are debugging
2 - Select that code
3 - Press CTRL + SHIFT + I
4 - Eclipse will evaluate your expression and show the results in a floating window
You can change variable values on the fly using the Debug perspective.
The top right corner of Eclipse should have the variables view from which you can select a variable name and change its value. You can also set conditional breakpoints by selecting the breakpoints view in the top right corner of Eclipse. Right-click on the breakpoint and select breakpoint properties. Check the conditional checkbox and write an expression that only when it evaluates as true does the program suspend during Debugging.
Change Variable Values during Debug
Conditional Breakpoints
Although Display View works; There are two ways else to evaluate an expression.
In the context of the debugging session, we can write and run custom code to evaluate possibilities. This is done in the Debug Shell. For example, if we need to cross-check the correctness of the sqrt functionality, we could do it in the Debug Shell. On the code, Right-click -> Inspect to see the value.
You can select an expression then open Context Menu -> Inspect or press CTRL + Shift + I, during debugging, the result will be displayed.
Select any expression in your code. Then right-click and select "Display" (or press the associated key-combination).
For completely new expressions, use the Display view. It's like a scrapbook for expressions. If the Display view is not visible, select Window > Show view > Display to add it. Then type any expression in the view, and do the same thing as for expressions in your code: select the expression, right-click and select "Display".
For expressions that you want to always see the value of, every time execution is halted, use the Expressions view instead.

Eclipse custom hotkey - print current line to standard out

Eclipse has a useful hotkey to assign current line to a local variable - when I type for instance:
Math.random()
and press ALT + SHIFT + L (Extract local variable), I can quickly change the line to
double random = Math.random();
I would like to use the same trick for printing it to std out, so that the Math.random() is being changed to:
System.out.println(Math.random());
Currently the fastet way to to this is to type syso and use content assist to use a template, but that requires manual copy pasting. Anyone knows a better way to do this?
Two options come to my mind to achieve your goal, but both of them require the selection of statement first.
After you select statement, press CTRL+SPACE, then type syso and hit Enter.
Selected statement will be placed inside System.out block:
System.out.println(statement);
Also you can prepare eclipse template (Window->Preference->Java->Editor->Content Assist->Templates), and give it some name:
System.out.println(${line_selection});${cursor}
After you select statement, press ALT+SHIFT+Z or select menu option Source->Surround With (also in context menu). Template you have created should be there so select it. Selected statement will be wrapped inside desired block of code.
As far as I am aware there is no shortcut available from the keys section of preferences. Is content assist really not fast enough for you in this case?

Eclipse text cursor has changed and editor behaves differently

I pushed a magic button and now my cursor (the blinking thing that shows where you type) split itself and now Eclipse is acting like a plain text editor/like Microsoft Word. The cursor, which usually looks like "|", now looks like "¦" (what Wikipedia calls a 'broken bar' or a 'parted rule' rather than a normal 'vertical bar').
I just want to know how to get Eclipse to act normal again, and what I accidentally did so I don't do it again.
It sounds like you have accidentally switched from Smart Insert mode to Insert mode.
Press Ctrl + Shift + Insert or tick Smart Insert Mode on the Edit menu to switch back.
Smart Insert is the feature that automatically insert closing quotes and brackets when you type the opener and places semi-colons at the end of the line when you press semi-colon (if you have the preference for that enabled.)
Maybe you pressed the insert key, which will change the Eclipse editor to Overwrite rather than Smart Insert (see the info bar at the bottom of the editor), and will change the cursor to a block rather than a vertical line?
Update: Thanks for clarification - see mikej's answer which is correct. I'll leave this answer in case anyone has the similar, related problem that I describe.
Just double click on "Smart Insert" / "Overwrite" in eclipse status bar
For reference, I am adding image.
If nothing works, restart eclipse. That is what I did !
Just click
Toggle Vrapper Icon in Toolbar..
That may causes some issues like these. Because I'm also faced the same issue until today.
Please refer the below images to get clear idea about this.
Make sure that it is in disable mode in status bar
On my windows computer I press Shift + 0 but the zero has to be on the numeric keypad not the top row numbers.
Shift + 0 toggle my cursor.
#Jeremy by saying " I have a vertical line with a gap in the middle of it, like the character above the forward slash " i guess you are referring to the ' Pipeline 'symbol, the one you use to denote' OR ' in programming languages. As people already answered, you are in Raw Insert Mode (as opposed to Smart Mode) so try Edit->Smart Insert Mode (Ctrl+Shift+Insert by default).
For more info you can visit this thread http://www.eclipse.org/forums/index.php/t/53833/
And remember, the symbol is called 'Pipe' symbol or 'Pipeline' symbol.
I also had same problem, you can fix this by pressing just INSERT button on keyboard (on windows platform) also.
It worked for me.
or you can restart your eclipse.
I've encountered this symptom in Eclipse 3.7 before. And after I restarted machine, it disappeared.
If your cursor symbol is " + " then press Alt+Shift+A.
or else we can use Edit menu options ==> Toggle Block Selections
If you are using spring tool suite then you can double click on highlighted Smart Insert . Some keyboards do not have insert button.
enter image description here

How do I get Netbeans to default to Hex display for watch variables?

In Netbeans 6.5 I am debugging a network stream and need to view the memory in Hex. The only way I have found to do this is to right click on each row and select display as Hex. This is very inefficient and I would like either a memory dump or default to hex. Is there a way to do this?
Looks like the context option you're using now is it. See the bottom of this netbeans.org page.
Questions on the Netbeans forums relating to a global hex display option in the debugger remain unanswered (for example).
You might try out a hex editor plugin.
Go to Tools -> Options -> Java Debugger -> Variable Formatters.
Click the “Add...” button.
In the “Add Variable Formatter” dialog enter:
Formatter name: “MyHexByte”. Class types: “java.lang.Byte”.
Value formatted as a result of code snipped:
"0x" +
java.lang.Integer.toHexString(byteValue() & 0xFF)
Click “OK”.
Click the “Apply “button in the Java Debugger dialog.
It's not particularly about individual bytes, but I keep a special watch in my watch expression list for when I need to see what a byte array holds:
new java.math.BigInteger(1, myByteArray).toString(16)
When I need it, I simply change the byte array name to whatever local variable I want to check. This hex representation matches what I see in my hex editor, which is what I need.

Categories