简体   繁体   English

如何在Java中找到在端口号上运行的进程的进程ID

[英]How to find the process id of a process running on a port number in java

I am new to java and window as well I want to kill the process which is running on a specific port. 我是java和window的新手,我想杀死在特定端口上运行的进程。 let's say 9090. 假设9090。

what I tried 我尝试过的

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");
}

This is What you want to do . 这就是你想做的。 I hope it will help you. 希望对您有帮助。

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");
           }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在mac上找到正在运行的java进程的进程ID? - How to find the process id of a running java process on mac? "<i>How to find the process id of a running Java process on Windows?<\/i>如何在 Windows 上找到正在运行的 Java 进程的进程 ID?<\/b> <i>And how to kill the process alone?<\/i>以及如何单独杀死进程?<\/b>" - How to find the process id of a running Java process on Windows? And how to kill the process alone? 如何找到运行我的 Java 应用程序的进程 ID - How to find the process id that is running my Java Application 在linux中运行java进程,如何查找其参数,如其调试端口? - In linux for a running java process, how to find its parameters, such as its debug port? 如何找到杀死正在运行的Java VM的进程 - How to find the process that killed a running java VM 有没有办法找到进程使用的端口,给定其进程ID,使用java? - Is there a way to find a port being used by process, given its process id , using java? 如何找到当前正在活动端口监听的进程(使用Java) - How to find a process that is currently listening at an active port(using Java) 杀死在 java 中某个端口上运行的进程 - Kill process that is running on certain port in java 如何通过java程序获取exe的进程id - How to get a process id of exe running through java program 如何找到子进程的ID? - How to find the id of the child process?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM