Java代码操作win后台进程
话不多说,直接上代码
强调说明:
使用的时候程序要有管理员权限,也就是以管理员身份运行,运行IDE,或是运行打包好的黑窗口。
/**
* 代达罗斯之殇
*
* @ClassName: ProcessUtils
* @Description: 关于Java代码操作电脑后台进程的工具
* @author: zhangYuanLiang
* @date: 2022/5/12 11:05
*/
public class ProcessUtils {
/**
* 结束进程
*/
public static void killProcess(String pid) {
Runtime runtime = Runtime.getRuntime();
try {
//taskkill /pid 10580 -f
Process tasklist = runtime.exec("cmd.exe /c taskkill /f /t /im " + pid);
System.out.println("进程关闭成功" + pid);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 拿到PID对应的进程名
* @return
*/
public static String getPressName(String PID) {
ProcessUtils stringUtils = new ProcessUtils();
Runtime runtime = Runtime.getRuntime();
String processName = null;
int index = 0;
try {
Process tasklist = runtime.exec("tasklist");
BufferedReader reader = new BufferedReader(new InputStreamReader(tasklist.getInputStream(), "GBK"));
String line = null;
StringBuffer buffer = new StringBuffer();
while ((line = reader.readLine()) != null) {
if (index >= 3) {
// System.out.println(line);
String findpid = stringUtils.findpid(line);
if (PID.equals(findpid)) {
processName = stringUtils.getProcessName(line);
}
}
index++;
}
} catch (IOException e) {
e.printStackTrace();
}
return processName;
}
/**
* 返回端口号对应的PID
*
* @param Port
* @return
*/
public static String getPID(String Port) {
ProcessUtils stringUtils = new ProcessUtils();
Runtime runtime = Runtime.getRuntime();
String PID = null;
try {
Process findPort = runtime.exec("netstat -ano");
BufferedReader reader = new BufferedReader(new InputStreamReader(findPort.getInputStream(), "GBK"));
String line = null;
StringBuffer buffer = new StringBuffer();
while ((line = reader.readLine()) != null) {
// System.out.println(line);
String ipAndPort = stringUtils.findIPAndPort(line);
String ipAndPortE = stringUtils.findIPAndPortExternal(line);
String port = stringUtils.findPort(ipAndPort);
String portE = stringUtils.findPort(ipAndPortE);
if (port.equals(Port)) {
PID = stringUtils.findPID(line);
}
if (portE.equals(Port)){
PID = stringUtils.findPID(line);
}
}
} catch (IOException e) {
e.printStackTrace();
}
return PID;
}
/**
* 根据PID拿到进程名称
*/
public static String getProcessName(String line) {
String ss = line;
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < ss.length(); i++) {
if (ss.charAt(i) != ' ') {
buffer.append(ss.charAt(i));
} else {
break;
}
}
return buffer.toString();
}
/**
* 解析出PID
*/
public static String findpid(String line) {
String ss = line;
StringBuffer buffer = new StringBuffer();
for (int i = 33; i >= 0; i--) {
if (ss.charAt(i) != ' ') {
buffer.append(ss.charAt(i));
} else {
break;
}
}
return buffer.reverse().toString();
}
/**
* 拿到IP
*/
public static String findIPAndPort(String IP) {
String ss = IP;
StringBuffer ip = new StringBuffer();
for (int i = 9; i < ss.length(); i++) {
if (ss.charAt(i) != ' ') {
ip.append(ss.charAt(i));
} else {
break;
}
}
return ip.toString();
}
/**
* 拿到对外的ip
* @param IP
* @return
*/
public static String findIPAndPortExternal(String IP) {
String ss = IP;
StringBuffer ip = new StringBuffer();
for (int i = 32; i < ss.length(); i++) {
if (ss.charAt(i) != ' ') {
ip.append(ss.charAt(i));
} else {
break;
}
}
return ip.toString();
}
/**
* 拿到PID
*/
public static String findPID(String PID) {
String ss = PID;
StringBuffer pid = new StringBuffer();
for (int i = 71; i < ss.length(); i++) {
pid.append(ss.charAt(i));
}
return pid.toString();
}
/**
* 拿到端口号
*/
public static String findPort(String ipAndPort) {
String ip = ipAndPort;
StringBuffer buffer = new StringBuffer();
for (int i = ip.length() - 1; i >= 0; i--) {
if (ip.charAt(i) != ':') {
buffer.append(ip.charAt(i));
} else {
break;
}
}
return buffer.reverse().toString();
}
}
操作启动win程序
/**
* 代达 罗斯之殇
*
* @ClassName: StringUtils
* @Description: bat端口处理脚本
* @author: zhangYuanLiang
* @date: 2022/5/11 16:22
*/
public class UeUtils {
/**
* 启动指定端口号的ue实例
* @param port 端口号
*/
public static boolean startUE(String port) {
try {
String s = "cmd /c start E:\\NN" + port + "\\Windows\\NanNingTest_" + port + ".exe.lnk";
String ss = "cmd /c start E:\\PixelStreaming\\WebServers\\SignallingWebServer" + port + "\\platform_scripts\\cmd\\run_local.bat";
ExecuteScriptUtils.exCmd(ss);
ExecuteScriptUtils.exCmd(s);
return true;
}catch (Exception e){
return false;
}
}
/**
* 通过端口号来查杀进程, 杀死虚幻实例进程
* @param port 端口号
*/
public static boolean killProcessByPort(String port) {
try {
String pport = null;
if (port.equals("31")){
pport = "8001";
}else if ("32".equals(port)){
pport = "8002";
}else if ("33".equals(port)){
pport = "8003";
}
String pid = ProcessUtils.getPID(port);
String pidp = ProcessUtils.getPID(pport);
ProcessUtils.killProcess(pid);
ProcessUtils.killProcess(pidp);
ExecuteScriptUtils.closeCmd();
return true;
}catch (Exception e){
return false;
}
}
}