java實現關閉windos系統下的進程

package com.cmd.core;java


import java.io.BufferedReader;ide

import java.io.IOException;this

import java.io.InputStream;spa

import java.io.InputStreamReader;orm

import java.util.ArrayList;進程

import java.util.List;get


public class StreamGrabber extends Thread {  cmd

 

private final InputStream stream;  it

private final List<String> holder;  io

 

public StreamGrabber(InputStream stream) {  

 this(stream, null);  

}  

 

public StreamGrabber(InputStream stream, List<String> holder) {  

 this.stream = stream;  

 this.holder = holder;  

}  

 

@Override  

public void run() {  

 try {  

  BufferedReader br = new BufferedReader(  

    new InputStreamReader(stream));  

  String line = null;  

  while ((line = br.readLine()) != null) {  

   if (holder != null)  

    holder.add(line);  

  }  

 } catch (IOException ioe) {  

  ioe.printStackTrace();  

 }  

}  

 

public static Process performCommand(String command) {  

 try {  

  return Runtime.getRuntime().exec(command);  

 } catch (IOException e) {  

  e.printStackTrace();  

  return null;  

 }  

}  

 

public static void main(String[] args) {  

 // notepa.exe關閉記事本進程  

 String command = "taskkill /f /im 11Game.exe";  

 Process proc = performCommand(command);  

 List<String> outputs = new ArrayList<String>();  

 new StreamGrabber(proc.getInputStream(), outputs).start();  

}  

}  

相關文章
相關標籤/搜索