简体   繁体   English

如何从Java应用程序内部获取VM参数?

[英]How to get VM arguments from inside of Java application?

I need to check if some option that can be passed to JVM is explicitly set or has its default value. 我需要检查是否可以传递给JVM的某个选项是显式设置还是具有默认值。

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). 更具体一点:我需要创建一个具有比默认堆栈大更高的本机堆栈大小的特定线程,但是如果用户希望通过指定-Xss选项来自己处理这些事情,我想创建所有默认线程堆栈大小(将由用户在-Xss选项中指定)。

I've checked classes like java.lang.System and java.lang.Runtime , but these aren't giving me any useful information about VM arguments. 我检查过像java.lang.Systemjava.lang.Runtime这样的类,但这些都没有给我任何有关VM参数的有用信息。

Is there any way to get the information I need? 有没有办法获得我需要的信息?

At startup pass this -Dname=value 在启动时传递此-Dname=value

and then in your code you should use 然后在你的代码中你应该使用

value=System.getProperty("name");

to get that value 获得这个价值

With this code you can get the JVM arguments: 使用此代码,您可以获取JVM参数:

import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
...
RuntimeMXBean runtimeMxBean = ManagementFactory.getRuntimeMXBean();
List<String> arguments = runtimeMxBean.getInputArguments();

I found that HotSpot lists all the VM arguments in the management bean except for -client and -server. 我发现HotSpot列出了管理bean中除-client和-server之外的所有VM参数。 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. 因此,如果从VM名称推断出-client / -server参数并将其添加到运行时管理bean的列表中,则会获得完整的参数列表。

Here's the SSCCE: 这是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> . 如果你想要List<String>的参数,可以缩短。

Final note: We might also want to extend this to handle the rare case of having spaces within command line arguments. 最后注意:我们可能还想扩展它以处理在命令行参数中有空格的罕见情况。

I haven't tried specifically getting the VM settings, but there is a wealth of information in the JMX utilities specifically the MXBean utilities. 我没有尝试专门获取VM设置,但JMX实用程序中有大量信息,特别是MXBean实用程序。 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 http://java.sun.com/javase/6/docs/technotes/guides/management/mxbeans.html

如果你想要java进程的整个命令行,你可以使用: JvmArguments.java (使用JNA + / proc的组合来覆盖大多数unix实现)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM