Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a Java application detect that a debugger is attached?

Tags:

java

debugging

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?

like image 530
Heiko Rupp Avatar asked Mar 22 '11 15:03

Heiko Rupp


People also ask

How does a debugger work Java?

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.

Is debug enabled Java?

Click Java > JVM Settings tab. Under Debug Java Settings, select the Enable Debug checkbox.

Which command is used to debug a Java program?

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.


2 Answers

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.

like image 171
Daniel Avatar answered Oct 24 '22 12:10

Daniel


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

like image 33
kschneid Avatar answered Oct 24 '22 13:10

kschneid