How do I invoke the debugger from code? - java

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.

Related

Eclipse Debugging Run Methods

it's probably really easy but I still don't know how to do this.
Is it possible to run a method while a program in Java is running in debug-mode in Eclipse?
While debugging, open the "Display view" to run any code you want at the position you're currently at.
See:
https://help.eclipse.org/neon/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Freference%2Fviews%2Fdisplay%2Fref-display_view.htm
In debug mode, it will open a new perspective and it's going to show the workflow of your Main method, including all the methods invocations during it's execution, which variable's values has changed, everything that happen to the beginning until the end of your Main class code.
May you check this link, got a lot of usefull information.

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

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/

Change compiled code while debugging in Eclipse

It may sound stupid but: is it possible to tell Eclipse to change some already compiled code (in a .class file) while debugging?
I would like to check a couple of things for some values. I know that if it is compiled I cannot change it, so it makes no sense to try this. Well, in this concrete case it does make sense.
I am also aware of the hot code replace functionality of Eclipse but it doesn't work for me because it is for source files.
EDIT: In my case I don't want to change the value of a variable. I would like to put a 1 instead of a 0 in this call within a .class file
getTabFolder().setSelection(0);
If it is just a variable value you want to change you can do this.
When you are debugging in the debug view of eclipse there is a variables window. If you right click on the variable you wish to change and press 'Change Value' a window will pop up and you can change the value of that variable in there.
I think that you can change body of existing methods, but you cannot add new methods.
You can also change variable values.
It depends, if you are running the code locally Eclipse supports hot swapping.
You can find some info here.
If you change the code While debugging, eclipse will automatically transfer it to the target VM

Debugging using Java source code

Is there a way to include whole Java source code into an eclipse project so the program is easier to debbug (e.g. by inserting println in methods you otherwise couldn't insert anything)?
I have a bug in my code. But to better understand why the bug in my code appears, I'd like to see what intermediate results in some system method (on which use the bug occurs) are.
For example, I'd like to know what JViewPort.scrollRectToVisible() exactly does and how my input behaves in it by printing out some intermediate results that occur in the method itself.
EDIT:
Instead of using JRE System Library X, I want to add the source code from JDK as if I had written the code myself. I want to be able to edit any System class just as I am able to edit any class I created myself. I want editable .java files, not packed .jars...
You would need to add the 3rd-party library to your Eclipse workspace as an project. (How you would do that depends on the code you are dealing with.) Once you have done that, you can hack your copy of the library to add trace prints etcetera.
A better alternative is to simply attach the source code for the 3rd-party library so that the debugger can show you source as you step through the code, set breakpoints,. Then use "advanced breakpoint" techniques instead of trace prints; e.g. http://www.vogella.com/articles/EclipseDebugging/article.html#advanced
You cannot change the library code, but you can view it by using de-compiler. The max you can do is this. Now if you change any code in the libraries which you reached via the de-compiler, you would find an error stating "the integrity of the .jar package has changed which is not allowed"
Eclipse have built-in support for what you wanna do.
All you have to do is set breakpoint and execute application in debugging mode.
You can use these icons in Eclipse debugging perspective.
Follow along Eclipse Debugging Tutorial for details.

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