我是Java和Windows的新手,我想杀死在一个特定端口上运行的进程。比如说9090。
我试过什么
try{
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec("netstat -ano | findstr 9090");
BufferedReader stdInput = new BufferedReader(
new InputStreamReader(proc.getInputStream()));
String s = null;
if ((s = stdInput.readLine()) != null) {
int index=s.lastIndexOf(" ");
String sc=s.substring(index, s.length());
rt.exec("Taskkill /PID" +sc+" /T /F");
}
JOptionPane.showMessageDialog(null, "Server Stopped");
}catch(Exception e){
JOptionPane.showMessageDialog(null, "Something Went wrong with server");
}
最佳答案
这就是你想做的。我希望它能帮助你。
try{
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec("cmd /c netstat -ano | findstr 9090");
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(proc.getInputStream()));
String s = null;
if ((s = stdInput.readLine()) != null) {
int index=s.lastIndexOf(" ");
String sc=s.substring(index, s.length());
rt.exec("cmd /c Taskkill /PID" +sc+" /T /F");
}
JOptionPane.showMessageDialog(null, "Server Stopped");
}catch(Exception e){
JOptionPane.showMessageDialog(null, "Something Went wrong with server");
}
关于java - 如何在Java中找到在端口号上运行的进程的进程ID,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45147118/