user1131528
user1131528

Reputation: 161

How can I obtain the system properties for a particular JVM instance programmatically?

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?

Upvotes: 15

Views: 20605

Answers (3)

Nicolas Filotto
Nicolas Filotto

Reputation: 44965

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

Upvotes: 10

Somnath Musib
Somnath Musib

Reputation: 3714

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.

Upvotes: 0

Roger Lindsjö
Roger Lindsjö

Reputation: 11543

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

Upvotes: 22

Related Questions