最近遇到一个奇葩的问题,项目跑的好好的,没有安装其它特殊软件,突然服务器启动报错,日志如下,显然是服务器的8080端口占用了。
Caused by: java.net.BindException: Address already in use: bind
at sun.nio.ch.Net.bind0(Native Method)
at sun.nio.ch.Net.bind(Net.java:438)
at sun.nio.ch.Net.bind(Net.java:430)
at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:225)
at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:74)
at org.apache.tomcat.util.net.NioEndpoint.initServerSocket(NioEndpoint.java:218)
at org.apache.tomcat.util.net.NioEndpoint.bind(NioEndpoint.java:194)
at org.apache.tomcat.util.net.AbstractEndpoint.bindWithCleanup(AbstractEndpoint.java:1328)
at org.apache.tomcat.util.net.AbstractEndpoint.init(AbstractEndpoint.java:1341)
at org.apache.tomcat.util.net.AbstractJsseEndpoint.init(AbstractJsseEndpoint.java:241)
at org.apache.coyote.AbstractProtocol.init(AbstractProtocol.java:695)
at org.apache.coyote.http11.AbstractHttp11Protocol.init(AbstractHttp11
很多同学容易想到的常规解决方法(windows上)就是使用如下命令找到对应的pid,然后进行关闭
netstat -ano | findstr :8080
但是我这边用这个命令每次的结果都是空白的,也就是无法找到占用8080端口的进程。
那么windows端口占用,netstat无法找到,我们一般可以有哪些简单和行之有效的解决方法呢?
方法1 强制关机
!!!就是按住电源键5秒以上,系统断电。
有同学问为啥正常关机不就好了吗?实际上也是经验所得,正常关机有些进程会缓存下来,占用的端口也会在重启后立马就重新占用。但是强制关机所有的进程及程序缓存都会失效。
方法2 netstat 并使用windows10新增参数-q
通过查阅官网资料,从windows10开始,新增了一个-Q的参数能够查询出一种BOUND状态下的进程,笔者无法找到的进程就是这种状态的。
同时官网也推荐了使用powershell的Get-NetTCPConnection命令来找到对应的程序进程。
使用示例:
netstat -anobq | findstr :8080
官网资料:
Netstat has been updated in Windows 10 with the addition of the -Q switch to show ports that have transitioned out of time wait as in the BOUND state. An update for Windows 8.1 and Windows Server 2012 R2 has been released that contains this functionality. The PowerShell cmdlet Get-NetTCPConnection in Windows 10 also shows these BOUND ports.
Until 10/2016, netstat was inaccurate. Fixes for netstat, back-ported to 2012 R2, allowed Netstat.exe and Get-NetTcpConnection to correctly report TCP or UDP port usage in Windows Server 2012 R2. See Windows Server 2012 R2: Ephemeral ports hotfixes to learn more.
方法3 修改windows动态端口范围
有一些更新可能会使可使用端口的范围有变化,比如Hyper-v
可使用如下命令更新保留端口范围,保留端口范围应该包含你使用端口
netsh interface ipv4 show excludedportrange protocol=tcp
net stop winnat
net stop LanmanWorkstation
net stop WlanSvc
net stop WwanSvc
netsh int ipv4 add excludedportrange protocol=tcp startport=8080 numberofports=1
net start winnat
net start LanmanWorkstation
net start WlanSvc
net start WwanSvc
使用如下命令更新动态端口范围,该范围应该在你使用的端口范围外
netsh int ipv6 show dynamic tcp
netsh int ipv4 set dynamic tcp start=9001 num=16384