1

Is there a way to check whether we are running within the Intellij IDEA debugger from within the running/debugged Java code?

Matthias
  • 9,817
  • 14
  • 66
  • 125
  • 1
    Why do you need this information? – yole Feb 26 '15 at 23:15
  • 1
    @yole: Because I am developing a game that runs in full screen, which hides the mouse cursor. However, when debugging the code I want/need my mouse cursor. – Matthias Feb 26 '15 at 23:17
  • 1
    @yole One reason would be to show debugging information while debugging. – Ian Boyd Jul 26 '22 at 13:55
  • Another reason is to disable timeouts that would otherwise trigger when you're paused in the debugger to look around. – davidbak Aug 02 '23 at 18:07

1 Answers1

5

This probably covers the answer:

Can a Java application detect that a debugger is attached?

i.e. It's not in general possible to detect whether a debugger has connected, although Android specifically has such a capability: http://developer.android.com/reference/android/os/Debug.html#isDebuggerConnected%28%29

Here's an almost identical quesiton for Eclipse, with some workarounds - but as yole says, it depends upon what you are actually trying to do.

Determine if a java application is in debug mode in Eclipse

To determine if the app was launched using the debugger you could use this. A bit ugly, but it works:

// This will check if the Java Debug Wire Protocol agent is used.       
boolean isDebug = java.lang.management.ManagementFactory.
    getRuntimeMXBean().
    getInputArguments().toString().indexOf("jdwp") >= 0; 
Community
  • 1
  • 1
Donald_W
  • 1,773
  • 21
  • 35
  • boolean isDebug = java.lang.management.ManagementFactory.getRuntimeMXBean(). getInputArguments().toString().indexOf("jdwp") >= 0; // This will check if the Java Debug Wire Protocol agent is used. – Donald_W Feb 27 '15 at 00:13
  • Hey, that's awesome. It works very well. I will thus accept your answer, maybe you want to move your comment to the real answer... – Matthias Feb 27 '15 at 09:42
  • Thanks Matthias, will do – Donald_W Feb 27 '15 at 09:43