Java 如何确定是否启用了“调试模式”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3776204/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 04:49:05  来源:igfitidea点击:

How to find out if "debug mode" is enabled

javaeclipsedebugging

提问by Thomas Mueller

How can a Java program find out if it is running in debug mode?

Java 程序如何确定它是否在调试模式下运行?

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_TIMEOUThigher, 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.

应用程序在常规“全速”模式下的行为应与“调试模式”(当连接调试器时,在调试模式下运行时)略有不同。应用程序通过 TCP 与另一台计算机、另一个进程或自身内部进行通信。我的同事希望我们Socket.setSoTimeout(1000)默认使用,这样从套接字读取最多可以阻塞 1 秒。在调试时,这当然是不够的,应用程序将停止正常工作。所以一个解决方案是设置SO_TIMEOUT更高,但只是在调试模式下(例如:无限制)。现在,我并不总是设置断点,或者不想使用可以自己设置“调试”属性的调试版本。有时我会附加调试器(远程调试)。我主要使用 Eclipse,所以一个在那里工作的解决方案是可以的。

Possible answers include:

可能的答案包括:

  1. To find out if run in debug mode, use the following method in java.lang.management.*or javax.management.*...

  2. Your co-worker is wrong for reason X, you shouldn't set SO_TIMEOUTto 1 second by default.

  1. 要确定是否在调试模式下运行,请使用以下方法java.lang.management.*javax.management.*...

  2. 你的同事因为 X 原因错了,你不应该SO_TIMEOUT默认设置为 1 秒。

Update

更新

I know about the system property approach, but I leave the question open to solve my original question.

我了解系统属性方法,但我将问题悬而未决以解决我最初的问题。

采纳答案by Thomas Mueller

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 Protocolagent is used.

这将检查是否使用了Java Debug Wire Protocol代理。

回答by Sean Patrick Floyd

You're solving the wrong problem. Your program doesn't need to know this unless it's dealing with eclipse or jvm internals.

你在解决错误的问题。您的程序不需要知道这一点,除非它处理 eclipse 或 jvm 内部。

Solution

解决方案

Use a system property with a default value:

使用具有默认值的系统属性:

int timeout = Integer.parseInt( 
    System.getProperty("socket.timeout", "1000"));
socket.setSoTimeout(timeout);

And in the debug launch configuration, just add

在调试启动配置中,只需添加

-Dsocket.timeout=20000

to the call parameters

到调用参数

(If you don't specify the system property, the default value will be used)

(如果不指定系统属性,将使用默认值)

References

参考

回答by gustafc

Make the timeout configurable. The simplest way is to just use a system property and read it with Integer.getInteger:

使超时可配置。最简单的方法是只使用系统属性并使用以下命令读取它Integer.getInteger

private final static int SOCKET_TIMEOUT =
  Integer.getInteger("com.yourapp.module.socketTimeout", 1000); // default 1 sec

Then, when starting your app for debugging, just set the property from the command line (or an appropriate config file, depending on the environment your app runs in):

然后,在启动应用程序进行调试时,只需从命令行(或适当的配置文件,取决于应用程序运行的环境)设置属性:

java -Dcom.yourapp.module.socketTimeout=1000000 MainClass

This is good because it does not magically alter the behavior when you fire the app up in a debugger, and you can change the timeout when not debugging (for example, if you need to run it somewhere with a slow connection, some day).

这很好,因为当您在调试器中启动应用程序时,它不会神奇地改变行为,并且您可以在不调试时更改超时(例如,如果您需要在某天连接速度较慢的地方运行它)。

(Of course, if your system already uses a config file, it may be appropriate to add this value as an entry there instead.)

(当然,如果您的系统已经使用了配置文件,则可以将该值作为条目添加到那里。)

As to whether one second is an appropriate timeout... that depends completely on the app. Sometimes it's better to give a correct answer eventually, other times failing quickly is better than waiting for success.

至于一秒是否合适的超时......这完全取决于应用程序。有时最终给出正确答案更好,有时快速失败比等待成功更好。