How to learn from Java code if a VM is being debugged?

I want to have something like this:

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

So, when the debugger stops at a breakpoint, timeouts are not executed.

Are there any API or System / Environment properties to find out?

+4
source share
4 answers

The author of this thread found this solution:

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

A standard disclaimer seems appropriate, although it is rather fragile (in that it is very specific when it fires) and can lead to Heisenbugs (errors that do not appear when trying to debug). For many scenarios, it is possible that a system property or environment variable is better.

+2
source

You can create a new launch configuration (Run β†’ Run Configurations) and add an environment variable or system property there.

+1
source

if you are developing an RCP application that you can use:

 if(Platform.inDevelopmentMode()) { // .... } 
+1
source

This is possible by checking 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 if the port is listening or if there are connections.

+1
source

Source: https://habr.com/ru/post/1437338/


All Articles