193

I need to check if some option that can be passed to JVM is explicitly set or has its default value.

To be more specific: I need to create one specific thread with higher native stack size than the default one, but in case the user wants to take care of such things by himself by specifying the -Xss option I want to create all threads with default stack size (which will be specified by user in -Xss option).

I've checked classes like java.lang.System and java.lang.Runtime, but these aren't giving me any useful information about VM arguments.

Is there any way to get the information I need?

Matthias Braun
  • 32,039
  • 22
  • 142
  • 171
okutane
  • 13,754
  • 10
  • 59
  • 67

5 Answers5

230

At startup pass this -Dname=value

and then in your code you should use

value=System.getProperty("name");

to get that value

om-nom-nom
  • 62,329
  • 13
  • 183
  • 228
Java_Freak
  • 2,557
  • 1
  • 14
  • 2
  • 6
    I cannot use this to get -Xdebug – petertc Mar 04 '14 at 07:13
  • 39
    Not sure why this answer has been so upvoted, this only retrieves application parameters (specified with -D), not VM parameters (those specified with -X). The question is specifically about -X params. – cleberz Jan 19 '17 at 17:06
  • 11
    I came here because I believed parameters of type `-Dname=value` are JVM arguments and that there is no intrinsic difference to `-X` arguments. Actually, they are both passed to java and not the application at the command line and as evidence by example, in maven you can pass both as `-Drun.jvmArguments=...`. I believe that is why it is upvoted. – kap May 02 '17 at 11:49
  • 2
    This doesn't answer the original posters needs at all. – dan.m was user2321368 Sep 07 '17 at 14:56
  • 6
    Might be ! but it googles on tops when searching for "how to read VM options in code" and that's why it's relevant ) – 62mkv Oct 26 '17 at 07:50
  • 1
    A little more context: via IDEA Run/Debug Configuration for tests, it's not possible to set "program arguments", but it's fine to set VM options using "-Dname=value" form – 62mkv Oct 26 '17 at 07:51
  • In a regular Java application, `-Dname=value` goes in the VM Arguments (Run Configurations...=>select the application=>Arguments tab=>VM Arguments). – Andrew Nov 01 '17 at 19:06
  • I was searching for -Dname=value and I came here from google with searching "java get vm arguments" and this was the first post @cleberz thats why I voted up . – omerhakanbilici May 24 '19 at 07:46
  • @kap Hits the nail on its head. – Akito May 02 '22 at 16:07
226

With this code you can get the JVM arguments:

import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
...
RuntimeMXBean runtimeMxBean = ManagementFactory.getRuntimeMXBean();
List<String> arguments = runtimeMxBean.getInputArguments();
Askin Geeks
  • 303
  • 2
  • 5
  • 14
David Schuler
  • 2,375
  • 1
  • 14
  • 6
  • Sadly you cannot get the Name of the main class if it is given on the command line. – Daniel May 20 '10 at 08:06
  • @Daniel, this should get you the name of the main class: `final StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();` `final String mainClassName = stackTrace[stackTrace.length - 1].getClassName());` – laz Nov 16 '11 at 18:43
  • 4
    If you are guaranteed to be running on Oracle's JVM and have the code to parse the arguments that can be useful. – laz May 28 '12 at 12:57
  • 4
    @Vulcan That does not get VM arguments. It contains the main class name, and the args array to the main method. – dacongy Sep 24 '12 at 02:10
  • @dacongy Never did I imply that VM arguments are located in the `sun.java.command` system property; I said that property can be used to acquire main class name (although I did not mention, as @laz pointed out, that property is only present on Oracle's JVM). – FThompson Sep 24 '12 at 05:14
  • Just for the record, I had problems with Java 1.6.0_25 and JVM arguments with space characters in the value, this method was returning multiple entries for only one of these JVM args, problem didn't appeared in Java 1.7.0_21 – Jaime Hablutzel Mar 20 '14 at 03:31
  • @laz that assumes you are in the main thread – nafg Dec 26 '22 at 22:55
7

I found that HotSpot lists all the VM arguments in the management bean except for -client and -server. Thus, if you infer the -client/-server argument from the VM name and add this to the runtime management bean's list, you get the full list of arguments.

Here's the SSCCE:

import java.util.*;
import java.lang.management.ManagementFactory;

class main {
  public static void main(final String[] args) {
    System.out.println(fullVMArguments());
  }

  static String fullVMArguments() {
    String name = javaVmName();
    return (contains(name, "Server") ? "-server "
      : contains(name, "Client") ? "-client " : "")
      + joinWithSpace(vmArguments());
  }

  static List<String> vmArguments() {
    return ManagementFactory.getRuntimeMXBean().getInputArguments();
  }

  static boolean contains(String s, String b) {
    return s != null && s.indexOf(b) >= 0;
  }

  static String javaVmName() {
    return System.getProperty("java.vm.name");
  }

  static String joinWithSpace(Collection<String> c) {
    return join(" ", c);
  }

  public static String join(String glue, Iterable<String> strings) {
    if (strings == null) return "";
    StringBuilder buf = new StringBuilder();
    Iterator<String> i = strings.iterator();
    if (i.hasNext()) {
      buf.append(i.next());
      while (i.hasNext())
        buf.append(glue).append(i.next());
    }
    return buf.toString();
  }
}

Could be made shorter if you want the arguments in a List<String>.

Final note: We might also want to extend this to handle the rare case of having spaces within command line arguments.

Stefan Reich
  • 1,000
  • 9
  • 12
3

I haven't tried specifically getting the VM settings, but there is a wealth of information in the JMX utilities specifically the MXBean utilities. This would be where I would start. Hopefully you find something there to help you.

The sun website has a bunch on the technology:

http://java.sun.com/javase/6/docs/technotes/guides/management/mxbeans.html

reccles
  • 5,174
  • 3
  • 30
  • 31
2

If you want the entire command line of your java process, you can use: JvmArguments.java (uses a combination of JNA + /proc to cover most unix implementations)

user2179737
  • 493
  • 3
  • 6