maaartinus
maaartinus

Reputation: 46392

How to find out if a Java process was started in debugger?

I sometimes use a timer to call System.exit in order to kill my throw-away code snippet after few seconds, which is quite useful in case it eats 100% CPU and Windows gets irresponsible because of this. It's quite handy, except in case I start it in debugger. In debugger I'd like to disable it automatically, otherwise I forget it and my debugged process gets killed. Can I find out if a process was started in debugger?

Note: I know I should not use this for anything serious. I'm not going to.

Upvotes: 3

Views: 311

Answers (1)

Check here. This checks for the JDWP.

Basically:

boolean isDebug = java.lang.management.ManagementFactory.getRuntimeMXBean().
    getInputArguments().toString().indexOf("-agentlib:jdwp") > 0;

Upvotes: 5

Related Questions