Can you detect in Java if the code is being debugged?

Viewed 4323

Possible Duplicate:
How to find out if “debug mode” is enabled

Coming from C#, I cannot believe that Java has no way to execute code only when the program is being debugged. Somehow, Log4J seems to be able to do it. Does anyone know a possibility for this? I'm thinking of something like this:

#if DEBUG
    executedCode();
#endif

Or something like this:

if (Java.isDebugging())
    executeCode();

Ideas?

EDIT: Thanks to Matt Ball, the code in this possible duplicate works:

public static boolean debugging = java.lang.management.ManagementFactory.getRuntimeMXBean().
        getInputArguments().toString().indexOf("-agentlib:jdwp") > 0;
4 Answers
Related