java执行cmd命令的分装类

package com.utils.cmd;

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

/**
 *类名称:CmdUtil
 *类描述:执行cmd命令的封装类
 *创建人:少年阿斌
 */
public class CmdUtil {
    
    public static boolean existCourse(String course) throws Exception{
        String[] cmd=new String[]{"cmd.exe","/c","wmic process get name"};
            Process process=Runtime.getRuntime().exec(cmd);
            InputStream ins=process.getInputStream();
            BufferedReader reader=new BufferedReader(new InputStreamReader(ins));
            String line=null;
            while((line=reader.readLine())!=null){
                if(line.indexOf(course)!=-1){
                    return true;
                }
            }
        return false;
    }
    
    public static List<String> executeCmdReturnList(String cmd) throws Exception{
        List<String> result=new ArrayList<>();
            Runtime rt = Runtime.getRuntime();  
            Process pr = rt.exec("cmd /c "+cmd);
            BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream(), "GBK"));  
            String line = null;  
            while ((line = input.readLine()) != null) {
                result.add(line);
            }  
        return result;  
    }
    
    public static void executeCmd(String cmd) throws Exception{
        Runtime runtime=Runtime.getRuntime();
        runtime.exec("cmd /c "+cmd);
    }
    
    public static void restartPcNow() throws Exception{
        executeCmd("shutdown -r -t 1");
    }
    
    public static void restartPcForSecond(String second) throws Exception{
        executeCmd("shutdown -r -t "+second);
    }
    
    public static void shutdownPcForSecond(String second) throws Exception{
        executeCmd("shutdown -s -t "+second);
    }
    
    public static void shutdownPcNow() throws Exception{
        executeCmd("shutdown -s -t 1");
    }
    
    public static void cancelShutdown() throws Exception{
        executeCmd("shutdown -a");
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值