用Java来获取进程列表,统计某个进程个数,适用于windows和linux

18 篇文章 0 订阅
5 篇文章 0 订阅

开门见山,咱们直接上代码:

package com.yinxin.web;

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

/**
 * 算出进程列表,再统计出相应进程个数
 * @author syp
 *
 */
public class Process{
	/*
	 * 统计进程数量,适应与windows和linux
	 * @Author syp
	 * @param进程名称
	 * 返回值为进程个数
	 */
	public static int GetprocessNums(String processName) {
					
			Runtime runtime = Runtime.getRuntime();
			List<String> tasklist = new ArrayList<String>();
			java.lang.Process process=null;
			int count=0; //统计进程数
			try {
				/*
				 * 自适应执行查询进程列表命令
				 */
				 Properties prop= System.getProperties();
				 //获取操作系统名称
				 String os= prop.getProperty("os.name");
				 if (os != null && os.toLowerCase().indexOf("linux") > -1) {
				   //1.适应与linux  
			        BufferedReader reader =null; 
			        process = runtime.exec("ps -ef");				 
				//显示所有进程  
			          process = Runtime.getRuntime().exec("ps -ef");  
			            reader = new BufferedReader(new InputStreamReader(process.getInputStream()));  
			            String line = null;            
			            while((line = reader.readLine())!=null){ 
							if(line.contains(processName)){
					            System.out.println("line===>"+line);
								count++;		            	
							}	            	          	
			            }
					} else {
					//2.适应与windows
					process = runtime.exec("tasklist");
					BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
					String s = "";
					while ((s = br.readLine()) != null) {
						if ("".equals(s)) {
							continue;
						}
						tasklist.add(s+" ");
					}
					
					// 获取每列最长的长度
					String maxRow = tasklist.get(1) + "";
					String[] maxCol = maxRow.split(" ");
					// 定义映像名称数组
					String[] taskName = new String[tasklist.size()];
					// 定义 PID数组
					String[] taskPid = new String[tasklist.size()];
					// 会话名数组
					String[] taskSessionName = new String[tasklist.size()];
					// 会话#数组
					String[] taskSession = new String[tasklist.size()];
					// 内存使用 数组
					String[] taskNec = new String[tasklist.size()];
					for (int i = 0; i < tasklist.size(); i++) {
						String data = tasklist.get(i) + "";
						for (int j = 0; j < maxCol.length; j++) {
							switch (j) {
							case 0:
								taskName[i]=data.substring(0, maxCol[j].length()+1);
								data=data.substring(maxCol[j].length()+1);
								break;
							case 1:
								taskPid[i]=data.substring(0, maxCol[j].length()+1);
								data=data.substring(maxCol[j].length()+1);
								break;
							case 2:
								taskSessionName[i]=data.substring(0, maxCol[j].length()+1);
								data=data.substring(maxCol[j].length()+1);
								break;
							case 3:
								taskSession[i]=data.substring(0, maxCol[j].length()+1);
								data=data.substring(maxCol[j].length()+1);
								break;
							case 4:
								taskNec[i]=data;
								break;
							}
						}
					}				

					for (int i = 0; i < taskNec.length; i++) {
						//打印进程列表
						//System.out.println(taskName[i]+" "+taskPid[i]+" "+taskSessionName[i]+" "+taskSession[i]+" "+taskNec[i]);				
						if(taskName[i].contains(processName)){
							count++;//用于进程计数		
						}			
					}
				   }
							
				
			} catch (Exception e) {
				e.printStackTrace();
			}
			return count;			
			
		}
	
	
	public static void main(String[] args) throws InterruptedException {		
			int count=GetprocessNums("se.exe");
			System.out.println(count);	
	}

}

 

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

醉梦洛

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值