I wish to obtain the system properties set for a third party java process/JVM. I need to do this programmatically. For example getting the "java.class.path" property. How can I do this?
I know we can get properties for a java program that we write using System.getProperty(). But I need to get the system properties for a third-party JVM. How can I obtain the same?
You can determine the properties and variables of your JVM by determining the process id of java (ps -ef, jps, or task manager), cd'ing to $JAVA_HOME/bin directory, then running jinfo <process id> . Of course you can use grep to find a specific property.
The System class has two methods that you can use to read the system properties: getProperty and getProperties . The System class has two different versions of getProperty . Both retrieve the value of the property named in the argument list.
The system properties are actually stored in a PropertiesPropertySource which by default delegates to System. getProperties() as the source . That happens to be a synchronized Hashtable which implements Map .
If by third-party JVM you just mean another JVM then you should try jinfo. This will not work with all JVM implementations, but most probably have it or something similar. jinfo takes a process id as argument (or remote system, see man jinfo). To find the process id use jps or jps -v.
jinfo 74949 Attaching to process ID 74949, please wait... Debugger attached successfully. Server compiler detected. JVM version is 20.4-b02-402 Java System Properties: java.runtime.name = Java(TM) SE Runtime Environment sun.boot.library.path = /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Libraries java.vm.version = 20.4-b02-402 awt.nativeDoubleBuffering = true ...
Starting from Java 7, you can use the command jcmd
that is part of the JDK such that it will work the same way on all OS.
It can work with both a pid or the main class.
The syntax is then jcmd ${pid} VM.system_properties
Example:
> jcmd 2125 VM.system_properties
2125:
#Tue Jul 24 18:05:39 CEST 2018
sun.desktop=windows
...
The syntax is then jcmd ${class-name} VM.system_properties
Example:
> jcmd com.mycompany.MyProgram VM.system_properties
2125:
#Tue Jul 24 18:05:39 CEST 2018
sun.desktop=windows
...
More details about how to use jcmd
.
See also the jcmd
Utility
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With