How to get in Java 6+ the list of running JVMs on the localhost and their specs (i.e. Java version, running threads, etc.)?
Does the Java API provide such features? Is there a Java library that can do this?
Infinite! Multiple JVMs can run on a single machine, as for example, for execution of applets a separate JVM may exist and another JVM can be started by the User for execution of Java Byte Code, on a single machine.
Data types of JVM is, float, double, returnAddress and reference.
The jps tool lists the instrumented HotSpot Java Virtual Machines (JVMs) on the target system.
You can use jps
, a command-line tool distributed with the jvm. I'm not aware of any normal Java API for it, though. However, JConsole can do what you ask, so I had a look at its source. It was quite scary, but while looking around I found references to the jVisualVM classes, which are OK seeing as you specify Java 6+. Here's what I found:
The classes are all in sun.tools
, so firstly you have to find the jconsole.jar
that came with your JVM (assumes Sun JVM, of course) and add it to your classpath. Then a quick-n-dirty listing of the active VMs can be got like:
public static void main(final String[] args) {
final Map<Integer, LocalVirtualMachine> virtualMachines = LocalVirtualMachine.getAllVirtualMachines();
for (final Entry<Integer, LocalVirtualMachine> entry : virtualMachines.entrySet()) {
System.out.println(entry.getKey() + " : " + entry.getValue().displayName());
}
}
Once you've got a reference to your JVMs, you can communicate with them using the Attach API.
jps
in the \jdk\bin
directory prints a list of running Java processes but not their specs. Some running conditions are available:
C:\Java\jdk1.6.0_23\bin>jps -mlv
4660 -Dosgi.requiredJavaVersion=1.5 -Xms40m -Xmx512m -XX:MaxPermSize=256m
6996 sun.tools.jps.Jps -mlv -Dapplication.home=C:\Java\jdk1.6.0_23 -Xms8m
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