获取系统服务列表

获取windows的服务,启动服务,停止服务。

 

package notion.install.busi.check;

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

import notion.install.vo.ServiceVO;
import notion.vo.pub.exception.BusinessException;

/**
 * 系统服务业务类
 * @author ganxz
 * @date 2012-10-29
 */
public class SysServicesBusi {
 
 /**
  * 获取系统服务列表
  * @return
  * @throws BusinessException
  */
 public List<ServiceVO> getSysServiceList() throws BusinessException{
   BufferedReader bufferedReader = null;
         Process proc = null;
         try {
          
             proc = Runtime.getRuntime().exec("sc query state= all");
             bufferedReader = new BufferedReader(new InputStreamReader(proc.getInputStream()));
             String line = "";
            
             List<ServiceVO> rtnLst = new ArrayList<ServiceVO>();
             ServiceVO srcvo = null;
             while ((line = bufferedReader.readLine()) != null) {
              System.out.println(line);
             if(!line.equals("") && line.indexOf(":") >=0){
              String tmpstr = line.substring(0,line.indexOf(":")).trim();
              String tmpvalue = line.substring(line.indexOf(":") + 1).trim();
              if(tmpstr.equals("SERVICE_NAME")){
               srcvo = new ServiceVO();
               srcvo.setService_name(tmpvalue);
               rtnLst.add(srcvo);
              }else if(tmpstr.equals("DISPLAY_NAME")){
               srcvo.setDisplay_name(tmpvalue);
              }else if(tmpstr.equals("TYPE")){
               srcvo.setType(tmpvalue);
              }else if(tmpstr.equals("STATE")){
               if(tmpvalue.contains("STOPPED")){
                srcvo.setState(1);
               }else if(tmpvalue.contains("RUNNING")){
                srcvo.setState(4);
               }
              }
                }
             }
             for(ServiceVO src :rtnLst){
              System.out.println(src.getService_name() + ":" + src.getDisplay_name()+ ":" + src.getState());
             }
            
         } catch (Exception ex) {
             ex.printStackTrace();
            
         } finally {
             if (bufferedReader != null) {
                 try {
                     bufferedReader.close();
                 } catch (Exception ex) {
                 }
             }
             if (proc != null) {
                 try {
                     proc.destroy();
                 } catch (Exception ex) {
                 }
             }
         }
         return null;
 }
 /**
  * 启动服务
  * @param service_name
  * @throws BusinessException
  */
 public void startService(String service_name) throws BusinessException{
  try {
   Runtime.getRuntime().exec("net start " + service_name);
  } catch (IOException e) {
   throw new BusinessException("启动系统服务异常!原因:",e);
  }
 }
 /**
  * 关闭服务
  * @param service_name
  * @throws BusinessException
  */
 public void stopService(String service_name) throws BusinessException{
  try {
   Runtime.getRuntime().exec("net stop " + service_name);
  } catch (IOException e) {
   throw new BusinessException("停止系统服务异常!原因:",e);
  }
 }
 
 
 public static void main(String[] args) {
  
  try {
   new SysServicesBusi().startService("aaa");
  } catch (BusinessException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值