tags:

views:

44

answers:

3

A file contains the following:

HPWAMain.exe                  3876 Console                    1      8,112 K

hpqwmiex.exe                  3900 Services                   0      6,256 K

WmiPrvSE.exe                  3924 Services                   0      8,576 K

jusched.exe                   3960 Console                    1      5,128 K

DivXUpdate.exe                3044 Console                    1     16,160 K

WiFiMsg.exe                   3984 Console                    1      6,404 K

HpqToaster.exe                2236 Console                    1      7,188 K

wmpnscfg.exe                  3784 Console                    1      6,536 K

wmpnetwk.exe                  3732 Services                   0     11,196 K

skypePM.exe                   2040 Console                    1     25,960 K

I want to get the process ID of the skypePM.exe. How is this possible in Java?

Any help is appreciated.

+1  A: 

To find the Process Id of the application SlypePM..

  1. Open the file
  2. now read lines one by one
  3. find the line which contains SkypePM.exe in the beginning
  4. In the line containing SkypePM.exe parse the line to read the numbers after the process name leaving the spaces.
  5. You get process id of the process

It is all string operations.

Remember the format of the file should not change after you write the code.

Prav
Don't forget to close the file.
Dave Jarvis
A: 

Implementation in Java is not a good way. Shell or other script languages may help you a lot. Anyway, JAWK is a implementation of awk in Java, I think it may help you.

David Ge
+2  A: 

Algorithm

  1. Open the file.
  2. In a loop, read a line of text.
  3. If the line of text starts with skypePM.exe then extract the number.
  4. Repeat looping until all lines have been read from the file.
  5. Close the file.

Implementation

import java.io.*;

public class T {
  public static void main( String args[] ) throws Exception {
    BufferedReader br = new BufferedReader(
      new InputStreamReader(
      new FileInputStream( "tasklist.txt" ) ) );

    String line;

    while( (line = br.readLine()) != null ) {
      if( line.startsWith( "skypePM.exe" ) ) {
        line = line.substring( "skypePM.exe".length() );
        int taskId = Integer.parseInt( (line.trim().split( " " ))[0] );

        System.out.println( "Task Id: " + taskId );
      }
    }

    br.close();
  }
}

Alternate Implementation

If you have Cygwin and related tools installed, you could use:

cat tasklist.txt | grep skypePM.exe | awk '{ print $2; }'
Dave Jarvis