我知道(jvm)启动选项让jvm等到调试器连接 - 这不是我的意思.
是否有可能从Java代码中检测调试器的附件,以便我可以编写一个"脚本"来做一些事情,然后在某一点使我的应用程序等待调试器?
否.这些选项是JVM选项,在调试器连接之前不会执行任何Javacode.然而,您可以让应用程序启动,并在一个getter上为变量启动,您可以从调试器设置该变量以继续使用您的应用程序.
共识是:不会。Java 没有相当于:
但共识是:
同意最好的(非缩小的)黑客是:
public static boolean isDebuggerPresent() {
// Get ahold of the Java Runtime Environment (JRE) management interface
RuntimeMXBean runtime = java.lang.management.ManagementFactory.getRuntimeMXBean();
// Get the command line arguments that we were originally passed in
List<String> args = runtime.getInputArguments();
// Check if the Java Debug Wire Protocol (JDWP) agent is used.
// One of the items might contain something like "-agentlib:jdwp=transport=dt_socket,address=9009,server=y,suspend=n"
// We're looking for the string "jdwp".
boolean jdwpPresent = args.toString().contains("jdwp");
return jdwpPresent;
}
Run Code Online (Sandbox Code Playgroud)
在 Java 正式添加之前,这已经足够了。
归档时间: |
|
查看次数: |
4673 次 |
最近记录: |