74

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 ?

5
  • A system property is set when starting the JVM. Before that there is nothing to read. Commented Oct 20, 2014 at 6:52
  • Do you want to be able to distinguish between default system properties and those set/overridden via the command line?
    – Bohemian
    Commented Oct 20, 2014 at 6:53
  • Are you asking how to read system properties? Commented Oct 20, 2014 at 7:01
  • I need to know what are certain directories set e.g what is extension directory set currently
    – sakhunzai
    Commented Oct 20, 2014 at 7:13
  • You may wish to read System Properties and The Extension Mechanism Architecture if you haven't already, or you might just leap right in and try System.getProperty("java.ext.dirs"). Commented Oct 20, 2014 at 7:23

5 Answers 5

157

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 
4
  • 1
    It seems like VM.system_properties only prints "well known" properties. Is there a way to view properties that the user has created? Commented Jan 8, 2018 at 21:54
  • 2
    I don't know. I suggest you to post a question and link it to this one.
    – AdrianRM
    Commented Jan 12, 2018 at 15:06
  • how to get system properties in linux machine Commented Sep 18, 2021 at 7:03
  • I take it back, on my local Mac, I see system properties that are specific to my app. Commented Jan 25, 2023 at 0:24
31

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

1
  • how to get the value of java.ext.dirs ?
    – sakhunzai
    Commented Oct 20, 2014 at 7:27
6

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.

0

Before jcmd times one can use JMX management connection from VisualVM. It is UI app so you have to expose JMX port from your remote headless servers to the that info...

JMX settings might look like:

JAVA_OPT="$JAVA_OPT
-Dcom.sun.management.jmxremote
-Dcom.sun.management.jmxremote.rmi.port=$JMX_PORT
-Dcom.sun.management.jmxremote.port=$JMX_PORT
-Dcom.sun.management.jmxremote.host=localhost
-Dcom.sun.management.jmxremote.local.only=true
-Dcom.sun.management.jmxremote.ssl=false
-Dcom.sun.management.jmxremote.authenticate=false
-Djava.rmi.server.hostname=localhost"
0

Yes. No parsing is required.

jshell --feedback=silent -  <<< 'System.out.println(System.getProperty("java.io.tmpdir"))'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.