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");
}
}
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");
}
}