如何确定“调试模式"是否存在已启用 [英] How to find out if "debug mode" is enabled
问题描述
Java 程序如何知道它是否在调试模式下运行?
How can a Java program find out if it is running in debug mode?
应用程序在常规全速"模式下的行为应与调试模式"(当连接调试器时,在调试模式下运行时)略有不同.应用程序通过 TCP 与另一台计算机、另一个进程或自身内部进行通信.我的同事希望我们默认使用 Socket.setSoTimeout(1000)
,这样从套接字读取最多可以阻塞 1 秒.在调试时,这当然是不够的,应用程序将停止正常工作.因此,解决方案是将 SO_TIMEOUT
设置得更高,但只是在调试模式下(例如:无限制).现在,我并不总是设置断点,或者不想使用可以自己设置调试"属性的调试版本.有时我会附加调试器(远程调试).我主要使用 Eclipse,所以一个在那里工作的解决方案是可以的.
The application should behave a bit different in regular "full speed" mode than in "debug mode" (when a debugger is attached, when running in debug mode). The application communicates over TCP with either another computer, another process, or within itself. My co-worker wants us to use Socket.setSoTimeout(1000)
by default, so that reads from the socket can block for at most 1 second. When debugging, this is not enough of course, and the application stops working as it should. So a solution would be to set the SO_TIMEOUT
higher, but just in debug mode (for example: unlimited). Now, I don't always set breakpoints or don't want use a debug build where I could set the "debug" property myself. Sometimes I attach the debugger (remote debugging). I'm mainly using Eclipse so a solution that just works there is OK.
可能的答案包括:
要查看是否在调试模式下运行,请在
java.lang.management.*
或javax.management.*
中使用以下方法...
To find out if run in debug mode, use the following method in
java.lang.management.*
orjavax.management.*
...
由于 X 原因,您的同事错了,您不应将 SO_TIMEOUT
默认设置为 1 秒.
Your co-worker is wrong for reason X, you shouldn't set SO_TIMEOUT
to 1 second by default.
更新
我了解系统属性方法,但我将问题悬而未决以解决我最初的问题.
Update
I know about the system property approach, but I leave the question open to solve my original question.
推荐答案
我现在自己发现了:
boolean isDebug = java.lang.management.ManagementFactory.getRuntimeMXBean().
getInputArguments().toString().indexOf("jdwp") >= 0;
这将检查 Java Debug Wire使用协议代理.
这篇关于如何确定“调试模式"是否存在已启用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!