如何在java中查找在端口号上运行的进程的进程id

如何在java中查找在端口号上运行的进程的进程id,java,windows,cmd,Java,Windows,Cmd,我还不熟悉java和Windows,我想终止在特定端口上运行的进程。比如说9090 我试过的 try{ Runtime rt = Runtime.getRuntime(); Process proc = rt.exec("netstat -ano | findstr 9090"); BufferedReader stdInput = new BufferedReader( new InputStr

我还不熟悉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");
           }

请提供您在执行上述代码时得到的输出/错误。您能告诉我是否必须为mac实现相同的功能吗?我想要mac上的ti kill端口