如何找出是否“调试模式”启用 [英] How to find out if "debug mode" is enabled
问题描述
应用程序在常规全速模式下的运行方式与调试模式(在调试模式下运行调试器时)的行为有所不同。应用程序通过TCP与另一台计算机,另一个进程或其内部进行通信。我的同事希望我们默认使用 Socket.setSoTimeout(1000)
,以便从套接字读取最多1秒钟。调试时,当然不够,应用程序停止工作。所以一个解决方案是将 SO_TIMEOUT
更高,但只是在调试模式下(例如:unlimited)。现在,我不总是设置断点,也不想使用调试版本,我可以自己设置debug属性。有时我附加调试器(远程调试)。我主要使用Eclipse,所以一个只是在那里工作的解决方案是可以的。
可能的答案包括:
-
要查明是否在调试模式下运行,请在
java.lang.management。*
或javax.management。*
... -
你的同事错误的原因X,你不应该设置
SO_TIMEOUT
默认为1秒。
更新
我知道系统属性的方法,但我留下的问题是解决我原来的问题。
我现在发现了:
boolean isDebug = java.lang.management.ManagementFactory.getRuntimeMXBean ()。
getInputArguments()。toString()。indexOf(jdwp)> = 0;
这将检查使用Java Debug Wire Protocol 代理。
How can a Java program find out if it is running in debug mode?
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.
Possible answers include:
To find out if run in debug mode, use the following method in
java.lang.management.*
orjavax.management.*
...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.
I found it out myself now:
boolean isDebug = java.lang.management.ManagementFactory.getRuntimeMXBean().
getInputArguments().toString().indexOf("jdwp") >= 0;
This will check if the Java Debug Wire Protocol agent is used.
这篇关于如何找出是否“调试模式”启用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!