How do I skip execution of a line of code? [duplicate] - java

This question already has answers here:
How to modify Java code while running in debug mode?
(4 answers)
Closed 7 years ago.
I am debugging my application in Eclipse. Finally I reached a breakpoint in a state which will trigger the bug in the next line of code.
Due to a breaking change in the class, the most likely solution would be to remove that line of code.
Of course I could do that now: remove the line, recompile and reproduce the sitatuation again. However, since it is hard to reproduce the bug, can I simply skip the execution of that line of code now? E.g. can I set the "instruction pointer" in Eclipse?
Things I did:
I don't want F6 (Step Over), since that will execute the line.
Also "Run to cursor" is not what I want, since that will also execute the problematic line.
Comment out the code according to How to modify Java code during debugging, but that re-executed the method, thus changing the state
I have tried to find an answer on this question, but I don't only want to run a single line of code but all the rest.
This question only has answers which run code in between.
The drop to frame feature is also not helpful.
The linked questions are from 2009 to 2013, so I hope to get new answers.
I'm using Eclipse 4.5.1 (Mars.1), latest official version at the time of asking.

If you want execute the line after the one you want to skip. When you reach the line before the line to be skipped, select the code and right click, choose Inspect from menu. This will execute the selected code and provide you the result in a popup.
If you know the lines that have to be skipped. you can put them in an if statement and change the value of the boolean in the variables view in eclipse. And then proceed to other lines.
(using code from comments)
if (skipped){
//yourcode
};
It is also possible to create expressions in eclipse.
One thing which I missed out was eclipse also supports hot code swap. You can comment out the line and save it. It will drop the debugger control back to the starting of the method. Hot deploy has supported the code changes in the method implementation only. If you add a new class or a new method, restart is still required. If you have the server in eclispe, here is how you can make that possible http://www.mkyong.com/eclipse/how-to-configure-hot-deploy-in-eclipse/

Related

JUnit Stopping at Breakpoints in Files I Didn't Write and Don't Have Permission to

I am running unit tests on java code using intelliJ and junit. The unit tests were working fine, and they still are . . . until I run in debug mode. Today, when I run in debug mode, all of a sudden, they start iterating through java files that are installed with java, I didn't write, and that I don't have permission for like the following:
This is part of the java code base that I don't have any control over and I didn't set any breakpoint here. Yet it pauses here and makes me click through it to get past it. I wouldn't care if this was only a couple of additional clicks to click through, but I have clicked like 50 times and it still keeps going through base java code that I have no control over and is not what is throwing any problem or issue.
I tried changing the settings for code coverage but that didn't seem to do anything. Is there any way to get junit to only stop at breakpoints that I, myself, specified? Any help here would be appreciated. I didn't see a similar question on Stack Overflow and the stuff on other sites is all about crafting the unit test itself.
So crazy coder (see above) was correct, but I thought I would add (after painfully trying every other alternative) that you have to go to: Run | View Breakpoints and then scroll all the way down on the left side panel (which you may not notice if you have tons of breakpoints like I did) and at the bottom there are breakpoints for Java exceptions. You need to click those OFF see below:

Eclipse debugger stops the program in non-breakpoint line of it [duplicate]

This question already has answers here:
Eclipse Debugging - Stopping without a breakpoint [closed]
(3 answers)
Closed 7 years ago.
I am trying to debug a program written in Java. I used breakpoints to solve my program issues and I solved some of them and removed the breakpoints after that. Now every time I run the debugger it stops the program in a place that used to be a breakpoint (not any more), and because I have iterative program it takes me so much time to pass these let's say invisible breakpoints in debugger and reach the real breakpoints. I tried to put breakpoints and removed then,nothing changed. It happens only for some of removed breakpoints not all of them.
Any idea would be appreciated.
I am using :
Eclipse Version: 4.2.2
Build id: M20130204-1200
Java vesrion: 1.7.0_40-b43
Update:
Run->Remove All Breakpoints worked for me.
Make sure your java classes and war/ear/jar are in sync. To be on the safe side, delete the war/ear/jar, rebuild & deploy, this should resolve your issue

How do I invoke the debugger from code?

From Javascript, I can simply write
debugger;
and when that line executes, it stops the code as if I had put a breakpoint there.
Is there an equivalent in Java? I need it to work in Eclipse specifically.
EDIT: can we take it as read that I am not an idiot and if placing a breakpoint with the IDE itself were an option, I would have already done so?
FURTHER EDIT: I had not thought it necessary to point out that since placing a breakpoint with IDE is not an option, any answer that revolves around placing a breakpoint with IDE is not likely to be helpful. In case everybody is dying of curiosity, the original code is not written in Java -- it's processed down to Java byte-code. As a result, Eclipse is confused enough it doesn't want to set breakpoints.
The JVM debugger, which Eclipse uses (mostly) under the covers, can set breakpoint at a line number in a method IF compiled with certain optional debugging info OR at method entry (always).
If your classes were compiled without debugging "lines" so the debugger can't set a line breakpoint, and you don't want to or can't recompile them, you can still set a method-entry breakpoint. In Package Explorer -- NOT an edit window for the source -- right-click the method name/signature and Toggle Method Breakpoint to on.
This can be combined with the comment by #ajp: add a method e.g. void [static] debugger(){} that doesn't do anything when you call it, but provides a convenient target where you can set a method breakpoint.
Warning: although it is possible to compile with partial debugging info, like debugging "vars" but not debugging "lines", generally people just use "debug on" or "debug off". If your classes are compiled without debugging "vars", the debugger will be much less useful.
I am probably going to get a few downvotes, but so be it...
If you open a source file in Eclipse and right-click on the left edge of the document view, you will get the popup menu illustrated in the image below.
As you can see, you have the option to toggle a breakpoint and also to turn off and on the line numbers. So, I am not sure what you mean by "My Eclipse is being operated in an environment where it cannot find line numbers to the source code". Unless you have some modified version of Eclipse that does not show this menu, I don't know what you mean by that. The option is there.
You wrote:
From Javascript, I can simply write
debugger;
and when that line executes, it stops the code as if I had put a breakpoint there.
And also:
can we take it as read that I am not an idiot and if placing a
breakpoint with the IDE itself were an option, I would have already
done so?
Option 1: The simple, "incorrect" answer is that there is no instruction in the Java language to make the program pause in a breakpoint nor there is an option like in languages like C++ to
make a debug build. So, your "ONLY" option is to execute a breakpoint from the IDE.
Option 2: The complicated, correct answer is that you can do what you want following these instructions: https://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html
In your case, I don't believe that you don't have the option to place a breakpoint with the IDE to debug your program; no matter how complex your program is. BUT, I am not here to debate that point. According to your post, you have to do option 2 laid out here.

Java Code Debugging with step by step process highlighting with Eclipse

I saw this somewhere, but I don't know where. It was basically something in Eclipse that was a feature when debugging that highlighted the code as it went through the code. I need this because I have decompiled a huge library, and I need to know how specific features are created.
Thanks,
Neil
F5 key : Executes the currently selected line and goes to the next line in your program. If the selected line is a method call the debugger steps into the associated code.
More details in this link

Java set breakpoints programmatically

Is there any way to programmatically set breakpoints in Java?
Assume you have the filename with the source code line:
Test.java:123
How this can be done?
The Eclipse IDE does not allow you to set a breakpoint from your java code.
However, it does allow you to set conditional breakpoints. With a conditional breakpoint, you can tell Eclipse to only break on a line after some Java expression evaluates to true. You can only tell it to break after some number of iterations. These modes should suffice for almost every usecase.
To enable a conditional breakpoint, right-click on a breakpoint and go to "Breakpoint properties".
Back in the days of VisualAge Jave, I did this with
DebugSupport.halt()
This is something that would have to be supported by the IDE, and would break if the IDE dependencies were not present. As fas I know there are no IDEs today that support this.
I had the same problem but with 10000 files of java which i wanted to search for some string and put breakpoints based on that search.
You can generate xml file containing all breakpoints you need.
How to get that xml file structure?? simply go to debug mode --> right click ->Export breakpoints->then save the file anywhere.open that file and see how it is constructed.
what i did that i searched all the files line by line and generated that xml file and imported it to eclipse.
-You may wonder that how you can loop through 10000 file line by line as it will take a lot of time,you are right but what i did to overcome this is by inserting all lines into indexed field on mysql db.
-I know your case is not that complex but i hope it gives you an idea.you may come with something even better.
Most debuggers will let you break on an exception, so just create your own BreakpointException class, throw and immediately catch it. Have the debugger pause only on BreakpointException.

Categories