64

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.

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140

8 Answers8

83

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"

Aya Muhammad
  • 73
  • 1
  • 11
Aravind
  • 1,020
  • 8
  • 4
  • 2
    Thanks for the answer , as said i executed the following command on Windows Command Prompt C:\Users\kiran>netstat -anp | find 8086 It showed FIND: Parameter format not correct –  Jan 11 '12 at 10:59
  • 2
    You should cover the port number with in quotes. For example, netstat -ano | find "8086" – Aravind Jan 11 '12 at 12:18
  • Aravind , after issuing that command C:\Softwares\apache-tomcat-6.0.33\bin>netstat -anp | find "8080" Nothing came . Thank you . –  Jan 11 '12 at 12:57
  • 11
    I copy pasted the command and executed the same under Command Prompt , it displayed nothing . –  Jan 11 '12 at 13:47
  • 3
    Means no process occupied that port number. If u are not sure, try the TCP viewer from SysInternals and check whether the port is listed in it or not. – Aravind Jan 11 '12 at 13:50
  • Try this one in windows: netstat -ano | find "7" – apm Jul 31 '15 at 06:04
  • 11
    not working in windows 10. "netstat -ano|findstr port no" is working fine – Haz Pro Feb 09 '16 at 05:34
  • 1
    -an works but -anp does not work. FYI I am using win 10 Pro 64 bit – Rajesh Goel Jul 27 '18 at 18:14
  • I am trying to check if the exact port is in free or not. I used `netstat -an | find ":566"`. It is returning info that the port 56687 is in use. I tried with `an`,`ano`, `find` and `findstr`. I used `:` before the port number. But all returning the same results. OS am trying is Windows 10. – DeeJay007 Sep 27 '18 at 10:19
  • I tried all modifications like find/findstr/-an/-ano/:9200/9200 but non of them give a result. It silently ends up. Max I get an error "wrong parameter" if port number is not enclosed in "" – Čamo Apr 09 '19 at 09:38
  • for windows 10 netstat -ano|find ":port_no" , below answer is working – Nandha Frost Aug 18 '20 at 16:40
  • shoud use **command prompt**, not **PowerShell** – serge Jan 05 '23 at 15:27
50

It's netstat -ano|findstr port no

Result would show process id in last column

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Anup
  • 501
  • 4
  • 2
20

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
Nikhil Shaw
  • 334
  • 2
  • 7
  • 1
    `> nestat -ano | find ":443"` gives `netstat: illegal option -- o` – gen Mar 25 '20 at 11:21
  • This is the command that worked on Windows 7-64. Tested with XAMPP Apache server down, nothing came up on port 80. With same up, port 80 displayed an entry, as expected. – JayJay123 Jul 28 '21 at 05:22
6

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

fall
  • 984
  • 11
  • 33
2

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

  • its show this error Get-NetTCPConnection : No MSFT_NetTCPConnection objects found with property 'LocalPort' equal to '55283'. Verify the value of the property and retry. At line:1 char:2 + (Get-NetTCPConnection -LocalPort "55283").OwningProcess + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (55283:UInt16) [Get-NetTCPConnection], CimJobException + FullyQualifiedErrorId : CmdletizationQuery_NotFound_LocalPort,Get-NetTCPConnection – Abubakar Riaz Aug 17 '19 at 17:42
1

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

Masklinn
  • 34,759
  • 3
  • 38
  • 57
1

If someone is trying in windows 10 pro, netstat -anp might not list any connection, so try netstat -an and add | find to find your desire port

for example I want to check in port 65432 is being currently used then I do

netstat -an|find "65432"
mechbaral
  • 103
  • 7
0

netstat -ano| grep

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

krishnazden
  • 1,127
  • 10
  • 19