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. Writting 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 vmlist = new HashSet(local.activeVms());
     for (Object id : vmlist) {
       /\* 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. 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 :)

No idea how to do this in JDK 1.5 or backwards.



Comments:

Hi Vaibhav,

- sun.jvmstat.monitor.MonitoredHost (and jps) exists in 5.0 also and hence you can use it there.

- 6.0 also provides sun.tools.jconsole.LocalVirtualMachine class in jconsole.jar that will easily provide you access to all the running vmids alongwith the process names.

Thanks,
Kannan

Posted by Kannan on March 04, 2008 at 07:55 AM IST #

Wow ! I will try sun.tools.jconsole.LocalVirtualMachine for sure. Any idea of JDK 1.5 backward :) But we can cover most of the machine with 1.6 and 1.5.

Thanks for the information.

Posted by Vaibhav Choudhary on March 04, 2008 at 08:00 AM IST #

Hi Vaibhav,

- sun.jvmstat.monitor.MonitoredHost (and jps) exists in 5.0 also and hence you can use it there.

- 6.0 also provides sun.tools.jconsole.LocalVirtualMachine class in jconsole.jar that will easily provide you access to all the running vmids alongwith the process names.

Thanks,
Kannan

Posted by Kannan Balasubramanian on March 04, 2008 at 08:01 AM IST #

GOOD ONE

Posted by guest on March 15, 2008 at 07:46 AM IST #

hi
plz how do i khnow if the file has a headerPE valid and how recuperat the table of importation in java
execuse me for the language
svp a ce que vous pouvez me donner comment on verifie si on a un entete dos ou NTheader valide et comment on recupere la table d'importation on java
merci d'avence

Posted by maya on January 29, 2009 at 02:24 PM IST #

Hi,

Sorry, I didn't get your points. Try to explain me again :).

Posted by Vaibhav on January 29, 2009 at 03:16 PM IST #

thanks for your very soon answer..
well how we khnow that we have an executabl PE by analysing the headers of this PE and how we recuperat the import table of this PE in java
"this application is for detecting virus or program malicious malware by analysing fonction DLL"
I hope u interstand me now
(execuse me i have a bad englich if u speek french plz tell me is very esay for me to talk in french ) thanks again and really execuse me

Posted by maya on January 29, 2009 at 05:44 PM IST #

plz how we get a full path of all running process using java code
thanks

Posted by samia on February 08, 2009 at 05:17 AM IST #

plz answer me if you have a method to do this
urgen

Posted by samia on February 08, 2009 at 05:59 AM IST #

Hi Vaibhav,
Actually this code helped me but please tell me how to extract only process id from the entire task list

Posted by Mahendra on March 05, 2009 at 07:25 AM IST #

@Samia: you can extract full path from same place !

@Mahendra : id used in code is only process ID, remote rest of the things

Posted by Vaibhav Choudhary on March 09, 2009 at 04:08 AM IST #

hi thanks
well I want to extract a full path of all running process in windows,
and plz tell me if i can extract names off all process before running.
they tell me to work with hook but i don't interstand plz if you have any idea answer me plz

Posted by samia on March 31, 2009 at 10:26 PM IST #

you need to know the structure of Registry. if you know that, then you can read the registry and it will tell you the full path of the application.

Posted by Vaibhav Choudhary on April 01, 2009 at 02:51 AM IST #

hi
but how we do this plz

Posted by samia on April 01, 2009 at 06:29 AM IST #

this is cool, this is what we want dude......

Posted by links london on November 26, 2009 at 11:53 PM IST #

Hey Vaibhav

I've written a library to read and manage processes cross-platform in Java. It works on Windows, Linux, Solaris and Os X. You can also get CPU usage and memory information. Maybe you'll find it useful. It's at http://github.com/jezhumble/javasysmon.

Posted by Jez Humble on January 09, 2010 at 03:51 PM IST #

Hi Vaibhav,
Is it possible to get the JAVA process path from where it is getting executed.
For example
String processname = MonitoredVmUtil.mainClass(vm, true);
will give us the process name but is it possible to get the path from which the process is executed?.

Posted by uvaraj on September 30, 2010 at 04:17 AM IST #

Post a Comment:
Comments are closed for this entry.
About

Hi, I am Vaibhav Choudhary working in Sun. This blog is all about simple concept of Java and JavaFX.

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today