Monday, March 03, 2008

Listing Java Process from Java

Month Ago, one of my colleagues was making an application, actually the UI flavor of JStack. Since JStack asks you the process ID for attaching the particular Java Application with JStack. In a UI based tool, telling user to do Alt-Ctrl-Del and see the process ID,not sounds good. So, my first impression was that you have to show the java process in the drop down and user will select in that. Some day back even, I want to find all process running on my machine from java code for some stupid purpose. I am trying to write some code for both of them. Java can't play with system process and hence invoking a runtime is only solution to get all process and here it is:


import java.io.*;
class ListProcess {
public static void main(String[] args)throws IOException
{
Runtime runtime = Runtime.getRuntime();
String cmds[] = {"cmd", "/c", "tasklist"};
Process proc = runtime.exec(cmds);
InputStream inputstream = proc.getInputStream();
InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
BufferedReader bufferedreader = new BufferedReader(inputstreamreader);
String line;
while ((line = bufferedreader.readLine()) != null) {
System.out.println(line);
}
}
}


Code is written exclusively for Windows Machine :). And one line change in this code will list you only java running process.

String cmds[] = {"cmd", "/c", "jps"}; this is nothing but running jps.exe file in bin (jdk6 onwards). Its not all done. Writing Runtime code is not the real solution as there is little of platform dependencies. So, I have decide to write the code for getting List of Java Process. Again, I have checked by OpenJDK code for jps(search on jps.java file :) ) and I got some hint how to do it and here it goes:

import java.util.*;
import sun.jvmstat.monitor.*;

public class ListJavaProcess {
public static void main(String[] args) throws Exception {

/* Checking for local Host, one can do for remote machine as well */
MonitoredHost local = MonitoredHost.getMonitoredHost("localhost");
/* Take all active VM's on Host, LocalHost here */
Set ids = new HashSet(local.activeVms());
for (Object id : ids) {
/* 1234 - Specifies the Java Virtual Machine identified by lvmid 1234 on an unnamed host.
This string is transformed into the absolute form //1234, which must be resolved against
a HostIdentifier. */
MonitoredVm vm = local.getMonitoredVm(new VmIdentifier("//" + id));
/* take care of class file and jar file both */
String processname = MonitoredVmUtil.mainClass(vm, true);
System.out.println(id + " ------> " + processname);
}
}
}


I have written good amount of comment as it is all together a sun import rather than java or javax import(so no javadoc). This import resides in tools.jar, so even running simple javac and java will not work. So, running the program will go here:


E:\Program Files\Java\jdk1.6.0_10\bin>javac -classpath "E:\Program Files\Java\jd
k1.6.0_10\lib\tools.jar" ListJavaProcess.java


E:\Program Files\Java\jdk1.6.0_10\bin>java -classpath .;"E:\Program Files\Java\j
dk1.6.0_10\lib\tools.jar" Vista

3700 ------> ListJavaProcess

Right now only one java process is running. Now in the second code, you can play with some of the java process, but with native process in the above code you can't do anything except watching it :)

6 comments:

Anonymous said...

do you know a way to interact with cmd without "/c".
Im asking this because im trying to interact with gdb debugger, but the /c command its not available so i cant pass my arguments correctly.

thanks

Vaibhav Choudhary said...

it should not be very tough, can you tell me what is the command need to run, i mean give me a screen shot of commands.

Anonymous said...

thanks for answering....im having big trouble with this.
I got no problems calling cmd and passing commands like this:
String cm[] = {"cmd","/c","dir","&&","help"};
p = Runtime.getRuntime().exec(cm);

But when I interact with gdb like this:
String cm[] = {"gdb","file C:/programm.exe","run"};
p = Runtime.getRuntime().exec(cm);

i cant because everything passed after gdb is passed like arguments so i get an error, and i cant use the "/c" or "&&" comand i would use with cmd to pass multiple commands. Do you know any tip?

Anonymous said...

hi,
boss, i have a small prob.. i need to interact with batch file and maintain a session for ten min's(approx..). user will be passing dynamic queries. need to display the response to the user.. do u have any idea ....?

Anonymous said...

Thanks a lot, very useful

Anonymous said...

can i do the same with java own api code is it possible ??
my site