Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 946
user1131528 Avatar asked Jan 12 '12 10:01

user1131528


People also ask

How can I see JVM properties?

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.

Which method is used to retrieve system properties?

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.

Where are system properties stored in Java?

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 .


2 Answers

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
...
like image 147
Roger Lindsjö Avatar answered Oct 06 '22 01:10

Roger Lindsjö


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

like image 30
Nicolas Filotto Avatar answered Oct 06 '22 01:10

Nicolas Filotto