从命令行读取 Java 系统属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26459904/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 02:33:27  来源:igfitidea点击:

Reading Java system properties from command line

javaenvironment-variables

提问by sakhunzai

Is there a better way to print system properties from command line? As we can set the property e.g.

有没有更好的方法从命令行打印系统属性?因为我们可以设置属性,例如

 java  -D<name>=<value>  //set a system property

Without writing a class to do that?

不写一个类来做到这一点?

If not possible, why is it not possible/feasible/good to do that from the command line ?

如果不可能,为什么从命令行执行此操作是不可能/可行/好的?

采纳答案by AdrianRM

You can use the -XshowSettingsflag in the Hotspot JVM version 1.7 and up (not supported in 1.6):

您可以-XshowSettings在 Hotspot JVM 1.7 及更高版本(1.6 不支持)中使用该标志:

java -XshowSettings:properties -version

OpenJDK has had support for this flag since late 2010.

OpenJDK 自 2010 年末开始支持此标志。

Seen in http://marxsoftware.blogspot.de/2016/02/hotspot-jvm-XshowSettings.html

http://marxsoftware.blogspot.de/2016/02/hotspot-jvm-XshowSettings.html

EDIT 14 Dec 2016

编辑 2016 年 12 月 14 日

The Oracle JVM ships with the tool jcmd which allows you to see the flags present in a running JVM. See:

Oracle JVM 附带工具 jcmd,它允许您查看正在运行的 JVM 中存在的标志。看:

https://docs.oracle.com/javase/8/docs/technotes/guides/troubleshoot/tooldescr006.html

https://docs.oracle.com/javase/8/docs/technotes/guides/troubleshoot/tooldescr006.html

For this use case, you could use:

对于此用例,您可以使用:

jcmd <pid> VM.system_properties

But there are also many other useful commands. For example:

但是还有许多其他有用的命令。例如:

jcmd <pid> VM.flags
jcmd <pid> VM.command_line
jcmd <pid> GC.run 

回答by René Link

You can use jpsa tool that comes with the jdk. It can print out the system properties that were passed to a java process.

可以使用jdk自带的工具jps。它可以打印出传递给 java 进程的系统属性。

For example: On my system eclipse is running and

例如:在我的系统上 eclipse 正在运行并且

$ jps -v

outputs

产出

6632  -Dosgi.requiredJavaVersion=1.6 -Xms1024m -Xmx2048m -XX:MaxPermSize=512m

jpsis located in JDK_HOME/bin

jps位于 JDK_HOME/bin

EDIT

编辑

If you want all the properties use the jinfotool that is also located in JDK_HOME/bin. To use it you must know the process id of the java process you want to get information from. E.g.

如果您想要所有属性,请使用jinfo工具,该工具也位于JDK_HOME/bin. 要使用它,您必须知道要从中获取信息的 java 进程的进程 ID。例如

$ jinfo 6632

This tool also prints out the java.ext.dirs

该工具还打印出 java.ext.dirs

回答by ghud

If you need defaults that your JVM will initially have set unless overridden, use:

如果您需要 JVM 最初设置的默认值,除非被覆盖,请使用:

java -XshowSettings:properties -version  

This is helpful if you don't have a Java application already running, thus no pid to pass to one of the other commands.

如果您还没有运行 Java 应用程序,因此没有 pid 传递给其他命令之一,这将很有帮助。

If you are seeking the properties of a JVM already running that has properties set via default or set explicitly by command, then use the pid for that JVM found via jpswith the jcmdor jinfocommands as listed in answers above.

如果您正在寻找已运行的 JVM 的属性,该 JVM 具有通过默认设置或通过命令显式设置的属性,则使用通过上面答案中列出jpsjcmdjinfo命令找到的该 JVM 的 pid 。