idea启动之java.net.BindException: Address already in use: bind

时间:2024-04-25 07:06:38

背景:

在win10上安装docker desktop 后,重启电脑,打开idea,提示端口被占用。

一、遇到的问题

打开idea2022.3.3提示以下提示:

Internal error. Please refer to https://jb.gg/ide/critical-startup-errors

java.net.BindException: Address already in use: bind
    at java.base/sun.nio.ch.Net.bind0(Native Method)
    at java.base/sun.nio.ch.Net.bind(Net.java:555)
    at java.base/sun.nio.ch.ServerSocketChannelImpl.netBind(ServerSocketChannelImpl.java:337)
    at java.base/sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:294)
    at io.netty.channel.socket.nio.NioServerSocketChannel.doBind(NioServerSocketChannel.java:141)
    at io.netty.channel.AbstractChannel$AbstractUnsafe.bind(AbstractChannel.java:562)
    at io.netty.channel.DefaultChannelPipeline$HeadContext.bind(DefaultChannelPipeline.java:1334)
    at io.netty.channel.AbstractChannelHandlerContext.invokeBind(AbstractChannelHandlerContext.java:506)
    at io.netty.channel.AbstractChannelHandlerContext.bind(AbstractChannelHandlerContext.java:491)
    at io.netty.channel.DefaultChannelPipeline.bind(DefaultChannelPipeline.java:973)
    at io.netty.channel.AbstractChannel.bind(AbstractChannel.java:260)
    at io.netty.bootstrap.AbstractBootstrap$2.run(AbstractBootstrap.java:356)
    at io.netty.util.concurrent.AbstractEventExecutor.runTask(AbstractEventExecutor.java:174)
    at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:167)
    at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:470)
    at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:569)
    at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:997)
    at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
    at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
    at java.base/java.lang.Thread.run(Thread.java:833)
-----
Your JRE: 17.0.6+1-b653.34 amd64 (JetBrains s.r.o.)
D:\Program Files\JetBrains\IntelliJ IDEA 2022.3.3\jbr

原因:
端口被占用,在win10下安装docker desktop 重启电脑就出现上述问题。

文章讨论了使用Docker时遇到的一个问题。该问题涉及 Docker 容器的端口不工作,从而导致与访问权限相关的常见错误。根本原因是Windows中的“TCP动态端口范围”,Hyper-V的保留端口号引起了潜在的冲突。

二、解决方法

正确的解决方案是简单地重置“TCP动态端口范围”,以便Hyper-V只保留我们设置的范围内的端口。您可以通过以管理员权限运行以下命令将“TCP动态端口范围”重置为49152–65535,但如果您认为它太大,也可以将其更改为较小的范围。

方法1:

如果 6942 到 6991 之间的所有 50 个端口都被保留,被其他应用程序占用或防火墙不允许 IDE 绑定它们,则启动会失败,并出现以下异常:

解决方法:在管理员控制台 (cmd.exe) 中运行以下命令:

netsh int ipv4 set dynamicport tcp start=49152 num=16383 

netsh int ipv4 set dynamicport udp start=49152 num=16383

方法2:
如果上述方法没有帮助,请尝试以下命令:

net stop winnat

net start winnat

对于出现“无法分配请求的地址”错误的 macOS 用户:

请参阅https://apple.stackexchange.com/questions/332407/how-to-change-ifconfig-lo0,此命令应该有所帮助:

sudo ifconfig lo0 alias 127.0.0.1

附件

参考1:Revise IDE directory locking mechanism (don’t fail startup if all ports in range are taken, limited network due to firewall

参考2:Unable to bind ports: Docker-for-Windows & Hyper-V excluding but not using important port ranges

参考3:https://intellij-support.jetbrains.com/hc/en-us/community/posts/360004973960-Critical-Internal-Error-on-Startup-of-IntelliJ-IDEA-Cannot-Lock-System-Folders-