I know about the (jvm) startup options to have the jvm wait until a debugger is attached - this is not what I mean here.
Is it possible from within Java code to also detect attachment of a debugger, so that I could e.g. write a "script" that is doing some stuff and then at a certain point make my app wait for the debugger?
Debugging allows you to run a program interactively while watching the source code and the variables during the execution. A breakpoint in the source code specifies where the execution of the program should stop during debugging. Once the program is stopped you can investigate variables, change their content, etc.
Click Java > JVM Settings tab. Under Debug Java Settings, select the Enable Debug checkbox.
In order to setup the breakpoint, right click the line number on the left-hand side of the source code. Then, choose the “Breakpoint/Toggle Line Breakpoints” menu option to setup the breakpoint. Step 3 – Select the “Ctrl” and “F5” keys to start debugging the Java project.
No. The options are JVM options, and no Javacode is executed before the debugger connects. You can however let the app start, and spinloop on a getter for a variable, which you set from the debugger to let your app continue.
Depending on what you'd like to do, it might be worthwhile investigating the onthrow
JDWP sub-option. I haven't actually tried this ;-) but it seems like you could create a special exception type that you throw and catch to trigger JVM suspension. As shown in the linked examples, combining with launch
can provide for some interesting alternatives. Of course, the logic/workflow is different from what you've expressed, but it's something to think about...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With