如何从 Java 应用程序内部获取 VM 参数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1490869/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get VM arguments from inside of Java application?
提问by okutane
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.System
和这样的类java.lang.Runtime
,但是这些并没有给我任何关于 VM 参数的有用信息。
Is there any way to get the information I need?
有什么方法可以获得我需要的信息吗?
采纳答案by David Schuler
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();
回答by reccles
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:
Sun 网站上有很多关于该技术的信息:
http://java.sun.com/javase/6/docs/technotes/guides/management/mxbeans.html
http://java.sun.com/javase/6/docs/technotes/guides/management/mxbeans.html
回答by Java_Freak
At startup pass this -Dname=value
在启动时通过这个 -Dname=value
and then in your code you should use
然后在你的代码中你应该使用
value=System.getProperty("name");
to get that value
获得那个价值
回答by user2179737
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 实现)
回答by Stefan Reich
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>
.
如果您想要List<String>
.
Final note: We might also want to extend this to handle the rare case of having spaces within command line arguments.
最后一点:我们可能还想扩展它以处理在命令行参数中包含空格的罕见情况。