47

I want to change the logging level depending if I'm debbugging or not, but I can't find a code snippet to check if the application is running in debug mode.

I'm using eclipse to debug the application, so if the solution only works within Eclipse it will be fine.

Serxipc
  • 6,639
  • 9
  • 40
  • 51

7 Answers7

66

Found the answer on how-to-find-out-if-debug-mode-is-enabled

boolean isDebug = java.lang.management.ManagementFactory.getRuntimeMXBean().
    getInputArguments().toString().contains("-agentlib:jdwp");

This will check if the Java Debug Wire Protocol agent is used.

Denis Abakumov
  • 355
  • 3
  • 11
topgun_ivard
  • 8,376
  • 10
  • 38
  • 45
21

You could modify the Debug Configuration. For example add a special VM argument only in the Debug Configuration. You can use System.getProperties() to read the supplied arguments.

Even better, modify the configurations (Run and Debug) to load a different logging configuration file. It isn't good if you need to write code to determine the logging level. This should only be a matter of configuration.

Gama11
  • 31,714
  • 9
  • 78
  • 100
kgiannakakis
  • 103,016
  • 27
  • 158
  • 194
8

There is not an officially sanctioned way to reliably determine if any given JVM is in debug mode from inside the JVM itself, and relying on artifacts will just break your code some time in the future.

You will therefore need to introduce a methology yourself. Suggestions:

  • A system property.
  • An environment variable (shell variable like $HOME or %HOME%)
  • Ask the JVM about the physical location of a given resource - http://www.exampledepot.com/egs/java.lang/ClassOrigin.html - and based on it, make your decision (does the path contain the word "debug"? is it inside a jar or an unpacked class file? etc).
  • JNDI
  • The existance or content of a particular resource.
Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347
  • 1
    And sometimes you don't care about the code in the future, you just need to be able to debug it right now and the normal case will work just fine. – simpleuser May 03 '16 at 15:48
  • @user1663987 You still need to know these things in order to choose the most suitable method in any given situation. Please note that there is a vast difference between enabling debugging (which for all practical purposes is invisible to the program being debugged) and changing the log configuration - this is not something most programmers would expect influenced each other. – Thorbjørn Ravn Andersen May 03 '16 at 22:39
5

Have you tried add a vm argument in the eclipse run config?

Pass this as a VM Argument

-Ddebug=true

then you can do Boolean.getBoolean("debug") to check this.

DarkMental
  • 482
  • 7
  • 26
J-16 SDiZ
  • 26,473
  • 4
  • 65
  • 84
  • 1
    Boolean.getBoolean("debug") is stupid, because it tries to convert the String "debug" to bool, which results in false always. – Daniel Nov 01 '19 at 06:00
  • 1
    @Daniel this is misleading as the JavaDocs says about this method: Returns true if and only if the system propertynamed by the argument exists and is equal to the string "true". Thus this solution works as long as this property is set. – Sebastian Götz Aug 20 '20 at 07:21
2

If you are setting the debug level from your own program, may be a line like:

public static final boolean DEBUG_MODE = System.getProperty("java.vm.info", "").contains("sharing");

would do the trick.

Just tested it in eclipse3.5:

package test;

public class Test
{

    /**
     * @param args
     */
    public static void main(String[] args)
    {
        System.out.println(System.getProperty("java.vm.info", ""));
    }

}

will display:

mixed mode, sharing

if launched without debug

mixed mode

if executed with debug launcher


Joachim Sauer comments:

This is highly system depending.
I assume the "sharing" indicates that cross-VM class-sharing is active.
This is a very new feature and is only available on some platforms.
Furthermore there can be many possible reasons to en- or disable it, so I wouldn't use this for debug-mode detection.

(Note: I tested it with the latest jdk1.6b14. I leave this as a CW answer.)

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • I was thinking there might be a property, but my tests of full dumps of properties from a debug launch and a run launch (using Eclipse 3.3, Java 1.6.0_13, Ubuntu 9.04) were identical. (I also tried just your property, including a debug run where I stepped over the System.out line, and both the Run and Debug launches gave 'mixed mode'. – gojomo Jul 10 '09 at 11:46
  • 1
    This is highly system depending. I assumie the "sharing" indicates that cross-VM class-sharing is active. This is a very new feature and is only available on some platforms. Furthermore there can be many possible reasons to en- or disable it, so I wouldn't use this for debug-mode detection. – Joachim Sauer Jul 10 '09 at 11:52
  • 1
    @Joachim: good point (I am with the latest jdk 1.6b14 here). I will update my answer and leave it for archive as Community Wiki. – VonC Jul 10 '09 at 13:30
  • Relying on the content of a string property indented for humans to read is at best brittle. – Thorbjørn Ravn Andersen Jul 11 '14 at 19:12
  • I've tested this with Oracle Java Hotspot jdk1.7.0 and jdk1.8.0 in Windows 10 and got the same "mixed mode" string both w debug and w/o debug, just as gojomo pointed out for 1.6 jdks. – cleberz Jan 19 '17 at 15:55
  • @VonC I fully understand why. As of Java 9 this does unfortunately not work. "mixed mode" is reported regardless. – Thorbjørn Ravn Andersen Nov 27 '17 at 12:42
  • @ThorbjørnRavnAndersen OK. Is there any solution specific to JDK9? – VonC Nov 27 '17 at 12:45
  • @VonC I haven't seen any yet. The getRuntimeMXBean approach still seems to work. – Thorbjørn Ravn Andersen Nov 27 '17 at 12:50
0

Have a look here:

http://wiki.eclipse.org/FAQ_How_do_I_use_the_platform_debug_tracing_facility%3F

Moreover, I think you can't know if your app is run in debug mode. The only thing you can do is to pass an argument to your JVM when you debug.

Manu

Manuel Selva
  • 18,554
  • 22
  • 89
  • 134
0

If using socket (e.g. 9999) you can call netstat to check if connection was established:

Process p = new ProcessBuilder("netstat", "-n").start();
String stdout = IOUtils.toString(p.getInputStream(), Charset.defaultCharset());

Then scan in stdout for 127.0.0.1:9999.*ESTABLISHED

radzimir
  • 1,178
  • 13
  • 10