java windows 下对进程的相关的操作

来源:互联网 发布:sql语句更改数据库名 编辑:程序博客网 时间:2024/05/28 05:16
/**
 * 
 */
package word;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;


/**
 * @author lizx
 * 
 */
public class KillWord {


/**

*/
public KillWord() {
// TODO Auto-generated constructor stub
}


/**
* @param args
* @author lizx Nov 18, 2011 9:46:16 PM
* @throws Exception 
*/
public static void main(String[] args) throws Exception {
KillWord
kw = new KillWord();
// kw.listRunningProcess();
// kw.openProcess();
// kw.openProcessOther();
//
// List list = kw.getProcessPid("WINWORD");
// System.out.println(list);

kw.killProces("chrome");
kw.listRunningProcess();
// boolean res = kw.findProcess("chrome.exe");
// System.out.println(res);
}


//
/**
* list all running process
* @author lizx 
*    Nov 18, 2011 11:11:47 PM
*/
public void listRunningProcess() {
BufferedReader br = null;
try {
Process proc = Runtime.getRuntime().exec("tasklist");
br = new BufferedReader(
new InputStreamReader(proc.getInputStream()));
@SuppressWarnings("unused")
String line = null;
System.out.println("打印所有正在运行的进程信息");
while ((line = br.readLine()) != null) {
System.out.println(br.readLine());
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}


//
/**
* open a process
* @author lizx 
*    Nov 18, 2011 11:12:22 PM
*/
public void openProcess() {
try {
Process proc = Runtime.getRuntime().exec("notepad");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}


//
/**
* open a process in an other way
* @author lizx 
*    Nov 18, 2011 11:12:52 PM
*/
public void openProcessOther() {
try {
String exeFullPathName = "C:/Program Files/Internet Explorer/IEXPLORE.EXE";
String message = "www.google.com";
String[] cmd = { exeFullPathName, message };
Process proc = Runtime.getRuntime().exec(cmd);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//
/**
* cmd --> tasklist /?
* 筛选器:
    筛选器名        有效操作符                有效值
    -----------     ---------------           --------------
    STATUS          eq, ne                    正在运行 | 没有响应
    IMAGENAME       eq, ne                    图像名
    PID             eq, ne, gt, lt, ge, le    PID 值
    SESSION         eq, ne, gt, lt, ge, le    会话编号
    SESSIONNAME     eq, ne                    会话名
    CPUTIME         eq, ne, gt, lt, ge, le    CPU 时间,格式为
                                              hh:mm:ss。
                                              hh - 时,
                                              mm - 分,ss - 秒
    MEMUSAGE        eq, ne, gt, lt, ge, le    内存使用量(KB)
    USERNAME        eq, ne                    用户名,格式为 [domain\]user


    SERVICES        eq, ne                    服务名
    WINDOWTITLE     eq, ne                    窗口标题
    MODULES         eq, ne                    DLL 名


例如:
//    TASKLIST
//    TASKLIST /M
//    TASKLIST /V
//    TASKLIST /SVC
//    TASKLIST /M wbem*
//    TASKLIST /S system /FO LIST
//    TASKLIST /S system /U domain\ username /FO CSV /NH
//    TASKLIST /S system /U username /P password /FO TABLE /NH
//    TASKLIST /FI "USERNAME ne NT AUTHORITY\SYSTEM" /FI "STATUS eq running"


* @param processName
* @return
* @author lizx 
*    Nov 18, 2011 11:17:23 PM
*/
public static boolean findProcess(String processName){   
        BufferedReader br=null;   
        try{   
            //下面这句是列出含有processName的进程图像名   
        String command = "tasklist /FI " + " \"IMAGENAME eq "+processName +"\" ";
        //System.out.println(command);
            Process proc=Runtime.getRuntime().exec(command);   
            br=new BufferedReader(new InputStreamReader(proc.getInputStream()));   
            String line=null;   
            while((line=br.readLine())!=null){   
                //判断指定的进程是否在运行   
                if(line.contains(processName)){   
                    return true;   
                }   
            }   
               
            return false;   
        }catch(Exception e){   
            e.printStackTrace();   
            return false;   
        }finally{   
            if(br!=null){   
                try{   
                    br.close();   
                }catch(Exception ex){   
                }   
            }   
               
        }   
    } 
//
/**
* get Pids
* @return
* @author lizx 
*    Nov 18, 2011 10:40:23 PM
* @throws Exception 
*/
public List<String> getProcessPid(String processName) throws Exception{
Process proc = Runtime.getRuntime().exec("tasklist");
BufferedReader br = new BufferedReader(
new InputStreamReader(proc.getInputStream()));
@SuppressWarnings("unused")
String line = null;
String[] lineArr = new String[0];
List<String> list = new ArrayList<String>();
while( (line = br.readLine())!= null){
if(line.contains(processName)){
System.out.println(line);
lineArr = line.split("\\s+");
Utils.Print.ArrayPrint(lineArr);
list.add(lineArr[1]);
}

return list;
}


/**
* 杀死进程,1,纯dos下,开cmd窗口 ntsd -c q -p PID
* Runtime.getRuntime().exec("ntsd -c q -p 1528");

* tskill PID(process ID)
* Runtime.getRuntime().exec("tskill 3188");
* Kill process
* @param procesName
* @throws Exception
* @author lizx 
*    Nov 18, 2011 11:23:09 PM
*/
public void killProces(String procesName) throws Exception{
Process proc = Runtime.getRuntime().exec("tasklist");
BufferedReader br = new BufferedReader(
new InputStreamReader(proc.getInputStream()));
@SuppressWarnings("unused")
String line = null;
String[] lineArr = new String[0];
while( (line = br.readLine())!= null){
if(line.contains(procesName)){
lineArr = line.split("\\s+");
Runtime.getRuntime().exec("tskill "+lineArr[1]);
}

}
}
原创粉丝点击