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
最新推荐文章于 2025-01-24 17:35:40 发布
这段代码示例展示了如何使用Java的Runtime类执行系统命令,查找监听特定端口(这里是9090)的进程ID,然后使用Taskkill命令终止该进程。如果操作成功,将显示‘ServerStopped’,否则提示‘SomethingWentwrongwithserver’。
413

被折叠的 条评论
为什么被折叠?



