Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
124 views
in Technique[技术] by (71.8m points)

java - How to find out if "debug mode" is enabled

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:

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

question from:https://stackoverflow.com/questions/3776204/how-to-find-out-if-debug-mode-is-enabled

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...