4

I want to have something like this:

long timeout = isDebugModeActive() ? Long.MAX_VALUE : 10000;

So that when the debugger stops on a breakpoint the timeouts do not take place.

Is there any API or System/Environment property to find it out?

Ilya Shinkarenko
  • 2,134
  • 18
  • 35
  • You can get the input arguments via JMX and check if debugging was turned on. – Peter Lawrey Oct 02 '12 at 07:59
  • What I have done in the past is to create a timer which every 10 ms adds 10 ms (approx) to the time. When debugging, this timer slows down depending on how much stepping through code you are doing. It is likely to be overkill in most cases. ;) – Peter Lawrey Oct 02 '12 at 08:01

4 Answers4

2

The author of this thread found this solution:

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

A standard disclaimer seem appropriate, though - this is pretty brittle (in that it is very specific in when it is triggered) and can lead to Heisenbugs (bugs which don't appear when you're trying to debug). For many scenarios, a system property or environment variable is probably better.

Community
  • 1
  • 1
gustafc
  • 28,465
  • 7
  • 73
  • 99
1

You can make a new run configuration (Run -> Run Configurations) and add an environment variable or system property there.

mpartel
  • 4,474
  • 1
  • 24
  • 31
1

if you develop RCP application you may use:

if(Platform.inDevelopmentMode()) {
   // ....
}
aav
  • 2,514
  • 1
  • 19
  • 27
1

It is possible by inspecting the JVM arguments that started the program.

There you will see:

-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=[port]

Then you check is the port is listening or there are connections .

ssedano
  • 8,322
  • 9
  • 60
  • 98
  • this would work only for remote debugging sessions.. – Ilya Shinkarenko Oct 02 '12 at 08:26
  • excellent idea! at least on linux we can do this from within the java application `ps -A -o pid,ppid,cmd |grep "java.*YourMainPackageName[.]YourMainClassName" -i |tr ' ' '\n' |grep "[-]Xdebug"` – Aquarius Power Jan 18 '15 at 00:24
  • ah... even better, just java, the inspecting JVM args you said can be done with this tip: http://stackoverflow.com/a/1518250/1422630, I didnt know it was possible, thx! – Aquarius Power Jan 18 '15 at 00:28