13

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?

user1131528
  • 141
  • 1
  • 2
  • 5

3 Answers3

21

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
...
Roger Lindsjö
  • 10,912
  • 1
  • 39
  • 49
  • Thanks Roger!!! That would be the output I'm looking for :) But the jinfo command does not run on my JVM implementation. It asks for a specific flag and does not dump all Java System Properties as hoped. I am using Java 1.6.0.29 implementation on a Windows box. Is there another way out? – user1131528 Jan 13 '12 at 05:12
  • The documentation indicates that it should work the same on windows. Have you tried jinfo -flags or jinfo -sysprops ? – Roger Lindsjö Jan 13 '12 at 10:08
  • I did try both options..But sadly, it throws this message back at me :( Usage: jinfo – user1131528 Jan 14 '12 at 13:07
  • I tried 1.6.0_30 (from Oracle) on a Windows XP, and the jinfo there supports both -flags and -sysprops. Are you using a non Oracle JVM? Are you sure you are not using an jinfo from an older JVM? – Roger Lindsjö Jan 14 '12 at 13:41
  • This is a weird situation. I use 1.6.0.29 jinfo from Oracle on Windows XP. I really appreciate all the help extended even though the solution seems to evade me. I will try alternative approaches, if possible, and let you know if i succeed.. Thanks again! – user1131528 Jan 16 '12 at 04:13
5

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.

With the pid of the target JVM

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
...

With the class name

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

Nicolas Filotto
  • 39,066
  • 11
  • 82
  • 105
0

To programetically access the remote JVM statistics (JVM system parameters, Thread statististics, Memomy information, GC Information and other information), JMX can be used. For that, remote JVM must allow JMX connection (Check this on how to activate JMX in remote JVM). Basically you need following -D parameters to be set in the remote JVM with appropriate values:

-Dcom.sun.management.jmxremote.port=1234
-Dcom.sun.management.jmxremote.authenticate=false
-Dcom.sun.management.jmxremote.ssl=false
-Djava.rmi.server.hostname=127.0.0.1

Once the above setting is done, connect to the JMX port and get different Mbean information from the remote server from your application: Following is some sample code:

JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://" + HOST + ":" + PORT + "/jmxrmi");
JMXConnector jmxConnector = JMXConnectorFactory.connect(url);
MBeanServerConnection mbeanServerConnection = jmxConnector.getMBeanServerConnection();

With this mbeanServerConnection, you can access different managed beans and get the required information from the MX beans. For system properties, you need to get the RuntimeMXBean bean and invoke getSystemProperties() to get all system parameters.

Somnath Musib
  • 2,850
  • 2
  • 24
  • 40