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.

Michael Lorton
  • 43,060
  • 26
  • 103
  • 144
  • There's a specific debugger for each IDE, and you're looking for a breakpoint. – Luiggi Mendoza Nov 25 '14 at 19:28
  • 1
    No, in eclipse you set break points in the IDE. – thatidiotguy Nov 25 '14 at 19:28
  • Look at this http://eclipsetutorial.sourceforge.net/debugger01/lesson01.html – vzamanillo Nov 25 '14 at 19:30
  • 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. – ajb Nov 25 '14 at 19:34
  • 4
    [Java and Javascript are similar like Car and Carpet are similar](http://stackoverflow.com/questions/245062/whats-the-difference-between-javascript-and-java) – Bas Peeters Nov 25 '14 at 19:38
  • 1
    @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. – ajb Nov 25 '14 at 19:54
  • Is this a purely theoretical question or do you have an actual requirement for such a thing in your project ? In case the latter is true, could you please provide some sort of insight on that requirement ? – Costi Ciudatu Nov 25 '14 at 20:11
  • 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. – Michael Lorton Nov 25 '14 at 20:16
  • @Malvolio `System.out.println()` might be your only hope then. – SnakeDoc Nov 25 '14 at 20:26
  • http://blog.0x972.info/?d=2014/11/13/10/40/50-how-does-a-debugger-work -- java debuggers are not the same as gdb, but are similar enough in principal. You'll see why it would not be possible to set a breakpoint in code universally. – SnakeDoc Nov 25 '14 at 20:28
  • I was under the impression that Eclipse came with its own Java compiler, so there shouldn't be a problem with some other compiler failing to generate debug information (or being told not to generate it). At least I'd think ... If you get Eclipse to rebuild the source, would it then be able to find line number info? – ajb Nov 25 '14 at 20:45
  • How are you invoking your application? If it's a web application, you need to be running it out of Eclipse in order to use the debugger. For instance I use tomcat and have a tomcat plugin in eclipse that makes it very easy to debug/start/stop/etc.. – kfox Nov 25 '14 at 20:57
  • It's a webapp, and I'm attaching to the running process with Eclipse. – Michael Lorton Nov 25 '14 at 22:33

3 Answers3

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.

dave_thompson_085
  • 34,712
  • 6
  • 50
  • 70
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.

hfontanez
  • 5,774
  • 2
  • 25
  • 37
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.

hfontanez
  • 5,774
  • 2
  • 25
  • 37