Serxipc
Serxipc

Reputation: 6699

Determine if a java application is in debug mode in Eclipse

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.

Upvotes: 47

Views: 48987

Answers (7)

topgun_ivard
topgun_ivard

Reputation: 8564

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.

Upvotes: 69

radzimir
radzimir

Reputation: 1348

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

Upvotes: 0

kgiannakakis
kgiannakakis

Reputation: 104178

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.

Upvotes: 21

J-16 SDiZ
J-16 SDiZ

Reputation: 26910

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.

Upvotes: 5

VonC
VonC

Reputation: 1324723

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

Upvotes: 2

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.

Upvotes: 8

Manuel Selva
Manuel Selva

Reputation: 19050

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

Upvotes: 0

Related Questions