java中关闭某个程序的进程 cmd /c taskkill /pid 具体的某个pid
发布网友
发布时间:2022-04-11 22:38
我来回答
共2个回答
热心网友
时间:2022-04-12 00:07
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.LinkedList;
public class Command {
private java.lang.Process p;
private InputStream is;
private OutputStream os;
private BufferedWriter bw;
private BufferedReader br;
private ProcessBuilder pb;
private InputStream stdErr;
public Command() {
}
//获取Process的输入,输出流
public void setCmd(String cmd) {
try {
p = Runtime.getRuntime().exec(cmd);
os = p.getOutputStream();
is = p.getInputStream();
stdErr = p.getErrorStream();
} catch (IOException e) {
System.err.println(e.getMessage());
}
}
//向Process输出命令
public void writeCmd(String cmd) {
try {
bw = new BufferedWriter(new OutputStreamWriter(os));
bw.write(cmd);
bw.newLine();
bw.flush();
bw.close();
} catch (Exception e) {
e.printStackTrace();
}
}
//读出Process执行的结果
public String readCmd() {
StringBuffer sb = new StringBuffer();
br = new BufferedReader(new InputStreamReader(is));
String buffer = null;
try {
while ((buffer = br.readLine()) != null) {
sb.append(buffer + "\n");
}
System.out.println(p.waitFor());
} catch (Exception e) {
e.printStackTrace();
}
return sb.toString();
}
//将命令一股脑塞入list中
public LinkedList<String> doCmd(LinkedList<String> lists) {
LinkedList<String> list = new LinkedList<String>();
for (String s : lists) {
writeCmd(s);
list.add(readCmd());
}
return list;
}
public static void main(String[] args) {
Command cmd = new Command();
cmd.setCmd("cmd");
cmd.writeCmd("TASKKILL /PID 1230");
System.out.println(cmd.readCmd());
//LinkedList<String> list = new LinkedList<String>();
//list.add("dir/b");
//list = cmd.doCmd(list);
// for(String s:list){
// System.out.print(s);
// }
}
}
追问我是想按照PID关闭某个运行的进程 但好像不能成功 大侠能不能解释下你这段程序。。。。
热心网友
时间:2022-04-12 01:25
那应该是 java 没权限关闭,要先提升程序的权限追问应该不存在权限的问题。。。
追答百度 提升windows进程权限
或者WIN7下 UAC