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

时间:2023-01-27 23:07:08

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_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.

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

Possible answers include:

可能的答案包括:

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

    要了解是否在调试模式下运行,请在java.lang.management。*或javax.management中使用以下方法。* ...

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

    你的同事因为X而错,你不应该默认将SO_TIMEOUT设置为1秒。

Update

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

我知道系统属性方法,但我保持问题解决我的原始问题。

3 个解决方案

#1


67  

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.

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

#2


10  

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.

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

#3


5  

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

#1


67  

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.

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

#2


10  

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.

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

#3


5  

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