Find the PID of a process that uses a port on Windows

前端 未结 7 1189
没有蜡笔的小新
没有蜡笔的小新 2020-11-30 16:58

My service crash on startup with the classic:

java.rmi.server.ExportException: Listen failed on port: 9999

How can I find the process for k

相关标签:
7条回答
  • 2020-11-30 17:43

    If you want to do this programmatically you can use some of the options given to you as follows in a PowerShell script:

    $processPID =  $($(netstat -aon | findstr "9999")[0] -split '\s+')[-1]
    taskkill /f /pid $processPID
    

    However; be aware that the more accurate you can be the more precise your PID result will be. If you know which host the port is supposed to be on you can narrow it down a lot. netstat -aon | findstr "0.0.0.0:9999" will only return one application and most llikely the correct one. Only searching on the port number may cause you to return processes that only happens to have 9999 in it, like this:

    TCP    0.0.0.0:9999                        0.0.0.0:0       LISTENING       15776
    UDP    [fe80::81ad:9999:d955:c4ca%2]:1900  *:*                             12331
    

    The most likely candidate usually ends up first, but if the process has ended before you run your script you may end up with PID 12331 instead and killing the wrong process.

    0 讨论(0)
提交回复
热议问题