Since Java 1.5 Sun provides sources of jvmstat classes for JVM monitoring.
I've created simple program that is used these classes.
It detects all running JVMs and prints their information.
JavaPS.java
import java.util.Set;
import sun.jvmstat.monitor.HostIdentifier;
import sun.jvmstat.monitor.MonitoredHost;
import sun.jvmstat.monitor.MonitoredVm;
import sun.jvmstat.monitor.StringMonitor;
import sun.jvmstat.monitor.VmIdentifier;
public class JavaPS {
private static final String[] vmProperties = new String[]{
"java.property.java.vm.name",
"java.property.java.vm.vendor",
"java.property.java.vm.version",
"java.property.java.home",
"java.property.java.class.path",
"java.rt.vmArgs",
"java.rt.vmFlags",
"sun.rt.javaCommand"};
public static void main(String[] args) {
Set jvms;
MonitoredHost monitoredHost;
try {
monitoredHost = MonitoredHost.getMonitoredHost(new HostIdentifier("localhost"));
jvms = monitoredHost.activeVms();
} catch (Exception e) {
e.printStackTrace();
return;
}
for (Object jvm : jvms) {
int jvmid = (Integer) jvm;
System.out.println("Process ID: \t" + String.valueOf(jvmid));
try {
VmIdentifier id = new VmIdentifier("//" + jvmid + "?mode=r");
MonitoredVm vm = monitoredHost.getMonitoredVm(id, 0);
for (String vmProperty : vmProperties) {
System.out.println(vmProperty + ": \t"
+ ((StringMonitor) vm.findByName(vmProperty)).stringValue());
}
System.out.println();
monitoredHost.detach(vm);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
Aliases for virtual machine's properties have been taken from jdk1.5 sources (jdk-1_5_0-src-scsl.zip) from the file j2se\src\share\classes\sun\jvmstat\perfdata\resources\aliasmap.
Compile this code via command:
javac -classpath %JAVA_HOME%\lib\tools.jar JavaPS.java
and run:
java -classpath .;%JAVA_HOME%\lib\tools.jar JavaPS >JavaPS.txt
jvmstat package locates in tools.jar
Example of results output:
JavaPS.txt
Process ID: 2144
java.property.java.vm.name: Java HotSpot(TM) Client VM
java.property.java.vm.vendor: Sun Microsystems Inc.
java.property.java.vm.version: 1.6.0_03-b05
java.property.java.home: C:\Program Files\Java\jre1.6.0_03
java.property.java.class.path: .;E:\Devel\jdk1.5.0_09\lib\tools.jar
java.rt.vmArgs:
java.rt.vmFlags:
sun.rt.javaCommand: JavaPS
Process ID: 364
java.property.java.vm.name: Java HotSpot(TM) Client VM
java.property.java.vm.vendor: Sun Microsystems Inc.
java.property.java.vm.version: 1.6.0_03-b05
java.property.java.home: C:\Program Files\Java\jre1.6.0_03
java.property.java.class.path: SwingSet2.jar
java.rt.vmArgs:
java.rt.vmFlags:
sun.rt.javaCommand: SwingSet2.jar