Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I write to the Netbeans Debugger Console (in Java)?

Netbeans has a tabbed window in the Output section called "Debugger Console". Is it possible to write a message to this window using Java? If so, how?

like image 458
MatthewD Avatar asked Dec 04 '22 23:12

MatthewD


1 Answers

The messages you see in the debugger console are either

  1. informations given by the debugger itself (breakpoint added, for example)
  2. custom messages associated with a breakpoint

When you add a breakpoint to a line of code, the default behavior of the breakpoint is to suspend the thread that executed the line of code, and to print the text "Breakpoint hit at line {lineNumber} in class {className} by thread {threadName}.".

You can configure a breakpoint to print a custom text. This text will be output in the debugger console when it reaches the breakpoint. To do so, right click on a breakpoint, open the propoerties window, and enter your text in the field Print text.

A useful trick is to configure a breakpoint so that it does not block (suspend : no thread), and to enter a text. The effect is the same than adding println lines in your code, but the benefit is that you don't have to recompile the code, and it's easier to activate/deactivate these debugger logs (and it obviously does not stay on production code).

Note that, in the text of the breakpoint, you can use one of the special values {lineNumber}, {methodName}, {className} or {threadName}, and you can also evaluate some code with the syntax {=xxx}. Just replace xxx with a variable name, or a method call, or whatever.

like image 110
barjak Avatar answered Dec 31 '22 10:12

barjak