Published December 26th, 2008
Handling Java Code from another Java Code
Long back, I had written one blog on how to list Java Process running on System by Java Code. But with the new features of JDK6, you can not only see the list but can manage the other running Java Process. This is possible using class LocalVirtualMachine. This class has a list of methods :
connectorAddress,
displayName,
getAllVirtualMachines,
getLocalVirtualMachine,
isAttachable,
isManageable,
startManagementAgent,
toString,
vmid
Here I am just showing a simple code, which will again tell you all the running Java Process.
import sun.tools.jconsole.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
Map map = LocalVirtualMachine.getAllVirtualMachines();
Iterator iter = map.values().iterator();
LocalVirtualMachine vm = null;
while (iter.hasNext()) {
vm = (LocalVirtualMachine)iter.next();
System.out.println(vm.displayName());
}
}
}
A very very small code :). Note that this class is not in rt.jar so we need to add jconsole.jar and tools.jar in the classpath section.
So, here how you can run this code :
D:Program FilesJavajdk1.6.0_11binControlJavaApp>..javac -cp "D:Program Fi lesJavajdk1.6.0_11libjconsole.jar" Main.java D:Program FilesJavajdk1.6.0_11binControlJavaApp>..java -cp .;"D:Program F ilesJavajdk1.6.0_11libjconsole.jar";"D:Program FilesJavajdk1.6.0_11libt ools.jar" Main
Right now, in my system it is displaying:
Main org/netbeans/modules/javafx/preview/Main 1
which mean this code itself and netbeans code.
In next blog, I will try to show how to manage other running java code from a java code.











Java,JavaScript,Threading,
Optimization and more with Vaibhav 


