How do I know that my code is in debug mode in the IDE?

How can I write code that can run in runtime in debug mode differently (for example, it is called as “Debug As Java Application” in eclipse) from “run” mode (for example, it is called as “Run as Java application” ) in eclipse "). That is, for example, the code may print" Haha, you are debugging "when" Debugging as a Java application ", but does not print anything when" Run as a Java application "(I do not want to add any either arguments when calling the main method.) Is there any general method for implementing this that can work under any IDE, such as eclipse, IntelliJ etc.

+5
source share
2 answers

I implemented this as the following. I am studying the JVM options and looking at -Xdebug. See Code.

private final static Pattern debugPattern = Pattern.compile("-Xdebug|jdwp");
public static boolean isDebugging() {
    for (String arg : ManagementFactory.getRuntimeMXBean().getInputArguments()) {
        if (debugPattern.matcher(arg).find()) {
            return true;
        }
    }
    return false;
}

It works for me because eclipse runs an application that is debugged as a separate process and connects to it using JDI (Java debugging interface).

I would be happy to know if this works with other IDEs (e.g. IntelliJ)

+7
source

The difference between these modes is that in debug mode, your IDE will connect to the newly executed application with a debugger (usually through a binary socket). Having an attached debugger allows the IDE to detect hits with hits, exception, etc. This will not happen in normal mode.

Java.

0

All Articles