如何从java应用程序内部获取vm参数?

时间:2022-04-16 14:33:38

I need to check if some option that can be passed to JVM is explicitly set or has it's 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 -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 information about vmargs.

我检查过java.lang这样的类。系统和. lang。运行时,但这些并没有提供关于vmargs的信息。

Is there any way to get information I need?

有没有办法得到我需要的信息?

5 个解决方案

#1


150  

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

#2


157  

At startup pass this -Dname=value

在启动时,传递这个-Dname=value

and then in your code you should use

然后在你的代码中你应该使用

value=System.getProperty("name");

to get that value

得到这个值

#3


2  

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.

我还没有特别尝试获得VM设置,但是JMX实用程序中有大量信息,特别是MXBean实用程序。从这里开始。希望你能找到帮助你的方法。

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

#4


2  

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.

我发现HotSpot列出了管理bean中除-client和-server之外的所有VM参数。因此,如果您从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>.

如果想要列表 中的参数,可以将其缩短。

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

最后注意:我们可能还想要扩展它来处理在命令行参数中有空格的罕见情况。

#5


0  

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)

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

#1


150  

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

#2


157  

At startup pass this -Dname=value

在启动时,传递这个-Dname=value

and then in your code you should use

然后在你的代码中你应该使用

value=System.getProperty("name");

to get that value

得到这个值

#3


2  

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.

我还没有特别尝试获得VM设置,但是JMX实用程序中有大量信息,特别是MXBean实用程序。从这里开始。希望你能找到帮助你的方法。

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

#4


2  

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.

我发现HotSpot列出了管理bean中除-client和-server之外的所有VM参数。因此,如果您从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>.

如果想要列表 中的参数,可以将其缩短。

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

最后注意:我们可能还想要扩展它来处理在命令行参数中有空格的罕见情况。

#5


0  

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)

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