检查我们是否在Intellij IDEA Debugger

Check whether we are in Intellij IDEA Debugger

有没有办法从 running/debugged Java 代码中检查我们是否在 Intellij IDEA 调试器中 运行?

这可能涵盖了答案:

Can a Java application detect that a debugger is attached?

即通常不可能检测调试器是否已连接,尽管 Android 特别具有这样的功能:http://developer.android.com/reference/android/os/Debug.html#isDebuggerConnected%28%29

这是一个几乎相同的 Eclipse 问题,有一些解决方法 - 但正如 yole 所说,这取决于您实际尝试做什么。

Determine if a java application is in debug mode in Eclipse

要确定该应用程序是否已使用调试器启动,您可以使用它。有点难看,但它有效:

// This will check if the Java Debug Wire Protocol agent is used.       
boolean isDebug = java.lang.management.ManagementFactory.
    getRuntimeMXBean().
    getInputArguments().toString().indexOf("jdwp") >= 0;