Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 816
Thomas Mueller Avatar asked Sep 23 '10 07:09

Thomas Mueller


People also ask

How do I check if a debug port is open?

Try opening a command prompt and enter "netstat -a" to see a list of ports that are currently in use on your machine. Then pick a port that's not in use.

How can I tell if debugging is enabled in asp net?

In the Web. config file, locate the compilation element. Debugging is enabled when the debug attribute in the compilation element is set to true. Change the debug attribute to false to disable debugging for that application.


1 Answers

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.

like image 151
Thomas Mueller Avatar answered Sep 23 '22 06:09

Thomas Mueller