Eclipse Breakpoint System.out - java

typically I set a breakpoint in my Java application when I want to observe the run.
However sometimes I just want to know if a method is called or not. Therefore a breakpoint does not help me, and I insert a "systrace" statement
System.out.println("method signature");
I thought it would be a nice feature If I could set a breakpoint and when the breakpoint is reached to just print out the systrace message and continue the run.
Do you know if this is possible?

You have to make it a conditional breakpoint with a following condition:
System.out.printf(Thread.currentThread().getStackTrace()[1].getMethodName() + "\n") == null
Works fine in my Eclipse. I'm using printf to make printing code evaluate to boolean. Not sure if there's a way to automate inserting this code into a breakpoint.

Related

Debugging (setting break point) Ilog Jrules in evaluation part

I'm working with Jrules and ODM.
One of my evaluations fails due to a null pointer, and I want to know which particular condition failed.
How can I do this? The Jrules tutorials shows setting debug point in the action part, but not the evaluation (if...) part.
Is there a direct way to step condition by condition, so that I can locate which particular condition (evaluation) failed?
You can debug in the actions by putting some print statements in the initialaction and finalaction of each rules.
initialaction{
System.err.println("Debug here"+variable);
};
you can also add a sysout in the setter of the variable or loops used for actions and can see the system.out.log file inside sample server folder
(location: it depends in which drive we have installed the file) to trace what see what value is passing through that variable.
Thanks
Mohammed Khaleel

Start coding from the debugging point Eclipse

I have a code like this
String s="Test-Code-Data";
String[] splitedData = s.split("-");
I have a break point at second line. When my code reaches that point, can i start coding below that and immediately see the output. For example. I want to see the output from below code, and i type it when the execution reaches the second line only.
System.out.println(splitedData[1])
Is this possible in eclipse?
To answer your question, NO you cannot effectively alter code while the code is in execution without making Eclipse warn you that the code is 'out of sync'.
However, you can look at the actual values of your various variables in the various stages of execution of your code. And, once the execution is complete, then you can edit the code and Run the program in Debug again and keep going that way.
You can use the Inspect Variable feature to look at the value of a variable while in execution and paused at a breakpoint by selecting the variable you'd like to inspect and using the keyboard shortcut Ctrl+Shift+I to get the value of the variable.

During debugging, how to break execution after a specific line

I'm debugging a program and I have this line:
if (compareToName.equals(className)) {
Set<String> properties =
reflections.getResources(Pattern.compile(".*\\.properties"));
}
I have a breakpoint in the line that starts with Set<String>...
I want to know the value of properties after the execution of the line, but when I step over the line I'm over the block so the properties variable is not available in the variables window.
You're not going to get the value immediately after you step over that assignment because the value doesn't exist outside of the if statement. IntelliJ's debugger is still bound by the same rules of scope and variable lifetimes as other Java programs.
While I'm leery of this particular code snippet, getting the value isn't hard to do while in debug mode.
Alt+Left Click while hovering over a particular snippet (in this case, you'd hover over the dot to the right of the reflections bit.
Highlight the expression, then select "Evaluate Expression" (using the little calculator icon in the debug window). By default it will prepopulate that field with anything that is highlighted so that you can execute an evaluation of it.
Press Alt-F7 and enter the code you want to execute.
In your example:
reflections.getResources(Pattern.compile(".*\\.properties"));
you will see the result.

Inspect function of eclipse tricks me like a dummy, why does it seem to be that tricky while debugging?

Eclipse is awesome for writing java programs but today I find that it's awesome to trick new coders like me. #_#
I write a snippet as following,
public static void main(String[] args) {
for(int i=0;i<3;i++){
System.out.println("i = " + i);
}
}
then I add a breakpoint at the line of "System.out.print....", click "Debug" button, eclipse goes to the "Debug" perspective and the line of breakpoint is highlighted, then I move the cursor over variable "i", its value is "0" as expected.
And then, I select the "i++" and click "Inspect"(or press "Ctrl+Shift+I") once, I move the cursor over variable "i", its value changed to "1". I repeat "Inspect" again, the value of i changed to "2"......(its value will add by 1 every time I clicked the "Inspect")!!
Why does this happend!? I ONLY want to watch the value of "i" for debug propuse, DO NOT want to really change its value until I step into next statement. I think that "Inspect", as well as "Display" are only for viewing the variable/expression, they should not impact the value, but in this case, it doesn't work as I expect to.
Could anyone tell me what went wrong?
My eclipse version info:
Version: Indigo Service Release 2
Build id: 20120216-1857
If you inspect an expression, eclipse has to execute that expression so you can get the value. Therefore, if you inspect i++, eclipse adds one to i.
Think about it this way: If instead of i++, you inspected myFunction(i), would you expect eclipse to execute the function "myFunction" to get the value? It's the same with i++.
If you are concerned about displaying/showing values while debugging and do you want to be sure not affecting the value, you should select the variable or expression and use the "Watch" option.
This will track the variable/expression value without executing, just updating the new value each time this is run. I think is the most secure way.
As Pablo mentioned, it has to evaluate the code in order to tell you what value it returns. You could instead put a watch on "i+1" and that would give you the value you want without the side effect you don't want.
Basically, you need to be aware of any side effects of anything you launch, whether from the "main" code or from the debugging tools. As Erhannis mentioned, this is very useful at times for modifying values while debugging your code. (For example, you can verify that a tweak/fix is indeed helpful before actually tweaking your code.)
You were expecting "an isolate area" but this would be extremely hard to do, especially in an object-oriented context where many objects are linked to many other objects. Running the whole thing in parallel might sometimes work, but you'd lose the tweaking ability above. And in any context, you'd run into tons of problems with fighting over resources; e.g. both copies trying to read/write a particular file such as a log file. Also, the two execution paths could diverge, leading to incorrect/misleading watch values.
So, preventing such side effects is not really a feasible option here and would rarely be useful anyway. Just expect that the watches can both reflect and affect the code execution.

java why is long value in if statement validated wrong

In the below code the if statement evaluates to true but i can see its false.
Clearly there is a logic here to explain this but i cannot remember it.
2 movies showing the phenomenology:
http://www.youtube.com/watch?v=DFcRfPErfik
http://www.youtube.com/watch?v=NEC04-kLQBE
The line "holderCompleated.textInfo.setT..." will run even do diffHours==1
look at the picture, diffHours=1 and Expression windows show false
a summery for this question could be:
(dont be affraid to ask even the the silliest question :))
here's a picture when i enter the if statment
something strange going on. im adding the Log.d(TAG,... inside the if statement and the debugger is stepping over it. Also have a Log.d(TAG,.. just before the if statement and that one is ok.
Maybe the clean-up didn't work and you're running some old code. Did you try to restart Eclipse? And then maybe delete and re-import some projects?
This would also explain why you see the debugger entering the if statement: it isn't! The running code is just not synchronized with the source code.

Categories

Resources