在localhost上运行JVM的列表 [英] List of running JVMs on the localhost
问题描述
如何在Java 6+中获取localhost上运行的JVM列表及其规范(即Java版本,运行线程等)?
How to get in Java 6+ the list of running JVMs on the localhost and their specs (i.e. Java version, running threads, etc.)?
是否Java API提供了这样的功能?是否有可以执行此操作的Java库?
Does the Java API provide such features? Is there a Java library that can do this?
推荐答案
您可以使用 jps
,一个分发的命令行工具与jvm。但是,我不知道它有任何普通的Java API。但是,JConsole可以做你所要求的,所以我看看它的来源。这是非常可怕的,但在环顾四周时,我发现了对jVisualVM类的引用,当你指定Java 6+时,可以看到它们。这是我发现的:
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:
这些类都在 sun.tools
中,所以首先你必须找到<您的JVM附带的code> jconsole.jar (当然假设是Sun JVM)并将其添加到您的类路径中。然后可以得到一个快速脏的活动虚拟机列表:
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());
}
}
一旦你有了对JVM的引用,您可以使用附加API 与他们沟通。
Once you've got a reference to your JVMs, you can communicate with them using the Attach API.
这篇关于在localhost上运行JVM的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!