import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class SystemServiceOperation {
/**
* 重启系统服务
* @param serviceName
* @return
public static boolean restartService(String serviceName){
boolean result = true;
//关闭系统系统服务
result = result && stopService(serviceName);
//开启系统服务
result = result && startService(serviceName);
return result;
}
/**
* 开启系统服务
* @param serviceName 系统服务的名称
* @return
public static boolean startService(String serviceName) {
//记录日志的消息
String message = "";
//返回值
boolean error = true;
//执行过程
try {
Process process = Runtime.getRuntime().exec("cmd.exe /c net start "" + serviceName + """);
process.waitFor();
message += new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + "t";
message += "开启系统服务" + serviceName + "成功rn";
}
catch (Exception e) {
error = false;
message += new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + "t";
message += "开启系统服务" + serviceName + "失败rn";
}
System.out.println(message);
//返回
return error;
}
/**
* 关闭系统服务
* @param serviceName 系统服务的名称
* @return
public static boolean stopService(String serviceName) {
//记录日志的消息
String message = "";
//返回值
boolean error = true;
//执行过程
try {
Process process = Runtime.getRuntime().exec("cmd.exe /c net stop "" + serviceName + """);
process.waitFor();
message += new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + "t";
message += "关闭系统服务" + serviceName + "成功rn";
}
catch (Exception e) {
error = false;
message += new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + "t";
message += "关闭系统服务" + serviceName + "失败rn";
}
System.out.println(message);
//返回
return error;
}
/**
* 将服务注册成系统服务,并设置为开机自启动
* @param serviceName 服务名
* @param servicePath 服务安装目录绝对路径
* @return true为注册成功
public static boolean registSysService(String serviceName, String servicePath){
String message = "";
String cmd = "sc create " + serviceName + " binPath= ""+servicePath+""" + " start= auto";
try {
Runtime.getRuntime().exec(cmd);
message += "注册服务 "+serviceName+" 成功!!n";
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
message += "注册服务 "+serviceName+" 失败!!n";
return false;
}
try {
cmd = "net start "+ serviceName;
Runtime.getRuntime().exec(cmd);
message += "启动服务 " +serviceName + "成功n";
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
message += "启动服务 " +serviceName + "失败 n";
// //写日志文件
// LogOperation.writeLogsFile(message);
}
// //写日志文件
// LogOperation.writeLogsFile(message);
return true;
}
/**
* 从系统服务中移除指定名称的服务
* @param serviceName 服务名称
* @return 成功
public static boolean removeService(String serviceName){
String message = "";
String cmd = "sc delete "+serviceName;
try {
Runtime.getRuntime().exec(cmd);
message += "移除服务 "+serviceName+" 成功!!n";
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
message += "移除服务 "+serviceName+" 失败!! n";
// //写日志文件
// LogOperation.writeLogsFile(message);
return false;
}
// //写日志文件
// LogOperation.writeLogsFile(message);
return true;
}
public static void main(String[] args){
//SystemServiceOperation.registSysService("FoxMail", "D:\Program Files\Foxmail\Foxmail.exe");
// SystemServiceOperation.removeService("FoxMail");
// SystemServiceOperation.stopService("Apache Tomcat");
SystemServiceOperation.startService("Apache Tomcat");
}
}