Accessing JVM arguments from Java (to determine if JVM is running in debug mode) html

Accessing JVM arguments from Java (to determine if JVM is running in debug mode)

The other day, I was using the JDeveloper debugger to locally debug some code in an application that uses (and therefore becomes part of) an Oracle Coherence data grid. As it turned out, using step-by-step debugging in a JVM that is part of a Coherence cluster has significant impact on the rest of the grid. The other nodes see the node constantly alternating between being responsive and being frozen, and keep taking measures to ensure the "health" of the grid. As Coherence values "health" or better, "consensus" within the grid over response times, this regularly causes processing on the other nodes to temporarily stop as well.

To remedy this problem, I wanted to alter the "grid initialization" logic in my application to check whether the JVM is running in debug mode or not, and if so, to not do the grid initialization. I checked the java command line with which JDeveloper runs an application in debug mode, and found that there is a JVM argument  "-Xdebug" that is indicative of this fact. But as this is not a "custom argument", i.e. it does not follow the "-Dname=value" syntax, you won’t be able to obtain this argument at runtime using System.getProperty().

I went on a wild goose chase for a while, until I ran JConsole against my JVM and found an MBean property that held the JVM input arguments I was looking for. From there, it was a short path to the code that solved my problem:

 


  List inputArgs = ManagementFactory.getRuntimeMXBean().getInputArguments();
  if (!inputArgs.contains("-Xdebug"))
  {
    performGridInitialization();
  }

Although I needed this to solve a very specific problem, I think I’ll find other uses for this in the future. For instance, I could use this to dynamically alter log levels when running in debug mode, etc.

2 Comments

  1. Peter Stibrany December 5, 2008
  2. Satti K December 5, 2008