How to make sure that a certain Port is not occupied by any other process

WindowsNetworking

Windows Problem Overview


I am working on a Java EE Application in a Windows environment. (I am using Windows 7)

I am using Tomcat Server, unfortunately port number 8080 is busy (used by Oracle). Now I want to assign a different port to Tomcat.

So before changing inside conf/server.xml file, I want to make sure that a certain port is not occupied by any other process and it's free.

Windows Solutions


Solution 1 - Windows

You can use "netstat" to check whether a port is available or not.

Use the netstat -anp | find "port number" command to find whether a port is occupied by an another process or not. If it is occupied by an another process, it will show the process id of that process.

You have to put : before port number to get the actual output

Ex netstat -anp | find ":8080"

Solution 2 - Windows

It's netstat -ano|findstr port no

Result would show process id in last column

Solution 3 - Windows

netstat -ano|find ":port_no" will give you the list.
a: Displays all connections and listening ports.
n: Displays addresses and port numbers in numerical form.
o: Displays the owning process ID associated with each connection .

example : netstat -ano | find ":1900" This gives you the result like this.

UDP    107.109.121.196:1900   *:*                                    1324  
UDP    127.0.0.1:1900         *:*                                    1324  
UDP    [::1]:1900             *:*                                    1324  
UDP    [fe80::8db8:d9cc:12a8:2262%13]:1900  *:*                      1324
      

Solution 4 - Windows

It's (Get-NetTCPConnection -LocalPort "port no.").OwningProcess

Solution 5 - Windows

If you prefer Powershell, use this. You will get the name of the process.

PS C:\Users\Administrator> Get-Process -Id (Get-NetTCPConnection -LocalPort 9093).OwningProcess

Handles  NPM(K)    PM(K)      WS(K)     CPU(s)     Id  SI ProcessName
-------  ------    -----      -----     ------     --  -- -----------
   6021    1464  2760976    2131548     290.39  25512   2 java

The PID is in Id column, and it provides process name, as well.

If no process is using this port, you get a red error message.

If you want to kill the process with PID 25512, use

taskkill /PID 25512 /F

/F means force, some processes cannot be killed without /F

Solution 6 - Windows

If this is a purely local concern e.g. you want to run tomcat locally to test the application you're working on, something which often works is to configure port 0. In that case when the application port-binds it will be allocated a random "ephemeral" port by the OS, which hopefully it logs out.

You'll have to check if that's supported by tomcat, but I would expect it is given past answers mentioning it.

It avoids having to hardcode ports and issues of TOCTOU, though it is obviously somewhat less convenient as you need to get the port you need to connect to every time.

The alternative is to just try out a bunch of free ports e.g. 8000[0] and 8888 are common alternate ports for HTTP servers. 8008 is also an official IANA alternate port though I can't remember ever seing it used.

[0] though officially assigned to iRDMI

Solution 7 - Windows

netstat -ano| grep

this will give status of the port if being used or not

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser663724View Question on Stackoverflow
Solution 1 - WindowsAravindView Answer on Stackoverflow
Solution 2 - WindowsAnupView Answer on Stackoverflow
Solution 3 - WindowsNikhil ShawView Answer on Stackoverflow
Solution 4 - WindowsShivam TiwariView Answer on Stackoverflow
Solution 5 - WindowsfallView Answer on Stackoverflow
Solution 6 - WindowsMasklinnView Answer on Stackoverflow
Solution 7 - WindowskrishnazdenView Answer on Stackoverflow