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