How to get VM arguments from inside of Java application?

JavaJvmJvm Arguments

Java Problem Overview


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?

Java Solutions


Solution 1 - Java

At startup pass this -Dname=value

and then in your code you should use

value=System.getProperty("name");

to get that value

Solution 2 - Java

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();

Solution 3 - Java

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.

Solution 4 - Java

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

Solution 5 - Java

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)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionokutaneView Question on Stackoverflow
Solution 1 - JavaJava_FreakView Answer on Stackoverflow
Solution 2 - JavaDavid SchulerView Answer on Stackoverflow
Solution 3 - JavaStefan ReichView Answer on Stackoverflow
Solution 4 - JavarecclesView Answer on Stackoverflow
Solution 5 - Javauser2179737View Answer on Stackoverflow