java中 Process的使用

/**
 * 
 */
package com.jrs;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


/**
 * @CallCmd.java
 * created at 2013-4-17 上午10:31:22 by jrs521wlh
 * 
 * @author jrs521wlh jiangrushe2010@126.com
 * @version $Revision$</br>
 * update: $Date$
 */
public class CallCmd {
 public static void main(String[] args) {
 Runtime rt = Runtime.getRuntime();
 //执行CMD命令,能够云行本地程序
 Process p = null;
 
//		String path = "F:\\Program Files (x86)\\Tencent\\QQ\\QQProtect\\Bin\\QQProtect.exe";
// 
//		try {
//			p = rt.exec(path);
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
 
 
 //调用浏览器,可以打开指定的网站
//		String path="C:\\Users\\jrs521wlh\\AppData\\Local\\Google\\Chrome\\Application\\chrome.exe";
//		String chrome ="www.google.com";
//		String[] cmd={path,chrome};
//		try {
//			p = rt.getRuntime().exec(cmd);
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
 printProcess();
 System.out.println("开始查找进程");
 if(findProcess("QQ.exe")){
 System.out.println("查找进程存在");
 
 }else{
 System.out.println("不存在进程");
 
 }
 
 }
 
 //打印所有的进程信息
 public static void printProcess(){
 BufferedReader buff = null;
 
 Process p = null;
 
 try {
 //打印所有进程的信息
 p = Runtime.getRuntime().exec("tasklist");
 //用流读出来
 buff = new BufferedReader(new InputStreamReader(p.getInputStream()));
 
 System.out.println("打印进程系信息");
 
 String temp = null;
 //遍历
 while ((temp=buff.readLine())!=null) {
 System.out.println(buff.readLine());
 
 }
 
 } catch (IOException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 }finally{
 if(buff!=null){
 try {
 buff.close();
 } catch (IOException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 }
 }
 
 }
 
 }
 /**
 * 查看进程是否运行
 * @Title: findProcess
 * @data:2013-4-17上午11:09:26
 * @author:jrs521wlh
 *
 * @return
 */
 public static boolean findProcess(String processName){
 
 BufferedReader buff = null ;
 
 try {
 //下面这句是列出含有processName的所有进程图形名字
 Process p=Runtime.getRuntime().exec("tasklist"); 
 buff = new BufferedReader(new InputStreamReader(p.getInputStream()));
 String temp = null ;
 while((temp=buff.readLine())!=null){
 System.out.println(temp);
 if(temp.contains(processName)){
 return true;
 
 }
 
 }
 return false;
 } catch (IOException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 return false;
 }finally{
 if(buff!=null){
 try {
 buff.close();
 } catch (IOException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 }
 }
 
 }
 
 }
}

 
 
 

 

 

  
  
  
参考:http://blog.sina.com.cn/s/blog_6f561cc301011ji4.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值