简体   繁体   中英

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 eg 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM