http://www.rgagnon.com/javadetails/java-0593.html

유용한 팁이네요. 다른 운영체제는 또 다른 방법을 찾아야겠지만 ^^;

The Microsoft TASKLIST.EXE is used to dump the list of the currently running processes. It is similar to tasklist window but for the console.

From a Java program, we are launching TASKLIST.EXE and capture its output.

Note : TASKLIST.EXE is not included the HOME edition of XP. But you can download it from Web, for example : http://www.computerhope.com/download/winxp.htm.

import java.io.*;
import java.util.*;

public class WindowsUtils {
  public static List<String> listRunningProcesses() {
    List<String> processes = new ArrayList<String>();
    try {
      String line;
      Process p = Runtime.getRuntime().exec("tasklist.exe /fo csv /nh");
      BufferedReader input = new BufferedReader
          (new InputStreamReader(p.getInputStream()));
      while ((line = input.readLine()) != null) {
          if (!line.trim().equals("")) {
              // keep only the process name
              line = line.substring(1);
              processes.add(line.substring(0, line.indexOf(""")));
          }

      }
      input.close();
    }
    catch (Exception err) {
      err.printStackTrace();
    }
    return processes;
  }

  public static void main(String[] args){
      List<String> processes = listRunningProcesses();
      String result = "";

      // display the result 
      Iterator<String> it = processes.iterator();
      int i = 0;
      while (it.hasNext()) {
         result += it.next() +",";
         i++;
         if (i==10) {
             result += "\n";
             i = 0;
         }
      }
      msgBox("Running processes : " + result);
  }

  public static void msgBox(String msg) {
    javax.swing.JOptionPane.showConfirmDialog((java.awt.Component)
       null, msg, "WindowsUtils", javax.swing.JOptionPane.DEFAULT_OPTION);
  }
}

출처 : http://kogaeng.tistory.com/306

반응형

+ Recent posts