Reading Java system properties from command line

JavaEnvironment Variables

Java Problem Overview


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 ?

Java Solutions


Solution 1 - Java

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

java -XshowSettings:properties -version

OpenJDK has had support for this flag since late 2010.

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

EDIT 14 Dec 2016

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

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 

Solution 2 - Java

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

For example: On my system eclipse is running and

$ jps -v

outputs

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

jps is located in JDK_HOME/bin

EDIT

If you want all the properties use the jinfo tool 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 6632

This tool also prints out the java.ext.dirs

Solution 3 - Java

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

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.

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 jps with the jcmd or jinfo commands as listed in answers above.

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
QuestionsakhunzaiView Question on Stackoverflow
Solution 1 - JavaAdrianRMView Answer on Stackoverflow
Solution 2 - JavaRené LinkView Answer on Stackoverflow
Solution 3 - JavaghudView Answer on Stackoverflow