X

Vaibhav's Blog Space

  • Java
    January 6, 2009

Managing Java Process from Java Code

Guest Author

Back to Java :). Here is a small blog on how to manage Java Process from Java itself.

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, for running we need to use :

D:/Program Files/Java/jdk1.6.0_11/bin/ControlJavaApp>../javac -cp "D:/Program Fi
les/Java/jdk1.6.0_11/lib/jconsole.jar" Main.java
D:/Program Files/Java/jdk1.6.0_11/bin/ControlJavaApp>../java -cp .;"D:/Program F
iles/Java/jdk1.6.0_11/libjconsole.jar";"D:Program Files/Java/jdk1.6.0_11/lib/t
ools.jar" Main

Or we can modify classpath in Env. Variables.

Right now, in my system it is displaying:

Main
org/netbeans/modules/javafx/preview/Main 1

That means, this code itself and Netbeans is running as a java process.

In next blog, we will try to see how to manage(not listing) other running java code from a java code. I am not able to find too much of doc from net, so things are slow :).

Join the discussion

Comments ( 1 )