1

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.

13
  • 1
    No, in eclipse you set break points in the IDE. Commented Nov 25, 2014 at 19:28
  • 1
    If you need this in numerous places in the code, you can write your own debugger() method that does nothing, then set a breakpoint on that, so that you only have to set one breakpoint. Commented Nov 25, 2014 at 19:34
  • 4
    Java and Javascript are similar like Car and Carpet are similar Commented Nov 25, 2014 at 19:38
  • 2
    @ManeatingKoala So what? It's not at all uncommon to ask something like "XXX language has such-and-such feature, does YYY language have something similar"? That takes place even when the languages XXX and YYY don't have similar names. Commented Nov 25, 2014 at 19:54
  • 1
    I don't know if "requirement" is the word I would use. My Eclipse is being operated in an environment where it cannot find line numbers to the source code; therefore, it cannot place break-points. However, I can alter (in some cases) the source code, and the functional equivalent of a break-point would be very helpful. Commented Nov 25, 2014 at 20:16

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

enter image description here

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.