Java로 윈도우 서비스 리스트 가져오는 방법

2009. 2. 9. 12:49Java

아래의 방법을 통해 현재 윈도우 서비스에 등록된 리스트를 가져올수 있다.
현재 실행중인 프로그램 리스트는
task.exe 를 Runtime.exec로 실행해 해당 스트림을 통해서 가져올수 있다.

-------------------------------------------------------------------------------------------------------

There are at least 3 ways:
1. Access registry and read data from. You'll not receive status data (running, stopped etc.)

Windows services live under 
HKLM/SYSTEM/ControlSet001/Services

You can access the registry using
http://java.sun.com/j2se/1.5.0/docs/api/java/util/prefs/Preferences.html

Here's an example
http://www.rgagnon.com/javadetails/java-0421.html




2. Execute "sc query" command and parse output. There is no preinstalled "sc" on Win2K, you'll need to get one from XP or PSDK or somewhere.
 => 하지만 이것은 현재 실행중인 프로세스만 리스트에 출력된다.

=> 해당 프로세스의 대한 정보를 보려면 sc queryex 프로세스명이름  이라고 해야 해당 서비스의 세부 내용이 출력된다.
=> 자세한 것은 sc 를 콘솔에서 치면 샘플과 옵션이 세세하게 나온다.



3. Create your own JNI module with required native functionality. You need WinAPI experience for. I think you have no due to original question.


===============================================================

자아 이제 옵션으로 해당 프로세스를 통해 메세지를 어떻게 가져오는가?
아래는 대충 짜본 샘플 코드다. 필요한 사람은 가져다가 쓰시길~ ^^

아래 처럼 했을때 해당 출력내용이 스트림을 통해 line에 출력된다.
이 line String을 파싱해서 가져다 쓰면 된다.

  StringBuffer message = new StringBuffer();
  InputStreamReader isr = null;
  BufferedReader br = null;
  try {
   Process p = Runtime.getRuntime().exec(System.getenv("windir")+"\\system32\\"+"tasklist.exe");
   isr = new InputStreamReader(p.getInputStream());
   br = new BufferedReader(isr);
   
   String line = null;
   while ((line = br.readLine())!= null) {
System.out.println("msg=>"+line);
     }
  } catch (IOException e) {
   e.printStackTrace();
  } finally {
   if(isr!=null) try {isr.close(); } catch (IOException e) {}
   if(br!=null) try { br.close();} catch (IOException e) {}
  }


ncanis.tistory.com