Can a Java application detect that a debugger is attached?

余生长醉 提交于 2020-01-02 01:00:09

问题


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?


回答1:


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.




回答2:


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




回答3:


see Determine if a java application is in debug mode in Eclipse for detecting debugger

You could user TimerTask to poll for attachment



来源:https://stackoverflow.com/questions/5393403/can-a-java-application-detect-that-a-debugger-is-attached

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!