Programming codex

How To List All The Processes Running On A Machine From Java Code?

How To List All The Processes Running On A Machine From Java Code?

[ad_1]

A month back, I wanted to find out all processes running on my machine from java code for some stupid purpose. What d you do in such a scenario? I tried to write some code and was pretty successful. 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);

}

}
}

Mind you, the 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).

It’s not all done. Writing Runtime code is not the real solution as there is little of platform dependencies. So, I have decided to write the code for getting a list of Java processes. 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 vmlist = new HashSet(local.activeVms());

for (Object id : vmlist) {

/* 1234 – Specifies the Java Virtual Machine identified by limited 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 the class file and jar file both */

String process name = MonitoredVmUtil.main class(VM, true);

System.out.println(id + ” ——> ” + process name);

}

}
}

I have written a good amount of comment as it is altogether a sun import rather than java or java import. 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 FilesJavajdk1.6.0_10bin>javac -classpath “E:Program FilesJavajdk1.6.0_10libtools.jar” ListJavaProcess.java

E:Program FilesJavajdk1.6.0_10bin>java -classpath .;”E:Program FilesJavajdk1.6.0_10libtools.jar” ListJavaProcess 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 🙂

No idea how to do this in JDK 1.5 or backward (runtime is, of course, one option). Would love to learn it some other time.

[ad_2]

Source by Vaibhav Kumar Choudhary

Share on facebook
Share on google
Share on twitter
Share on linkedin
Share on pinterest
Share on whatsapp

Leave a Reply

Your email address will not be published. Required fields are marked *

Search:

Topics

Recent Posts