Tomcat无法启动:java.net.BindException: Cannot assign requested address。

12

当我启动本地Tomcat实例时,出现以下错误:

    SEVERE: StandardServer.await: create[8005]: 
    java.net.BindException: Cannot assign requested address: JVM_Bind
    at java.net.PlainSocketImpl.socketBind(Native Method)
    at java.net.PlainSocketImpl.bind(PlainSocketImpl.java:359)
    at java.net.ServerSocket.bind(ServerSocket.java:319)
    at java.net.ServerSocket.<init>(ServerSocket.java:185)
    at org.apache.catalina.core.StandardServer.await(StandardServer.java:373)
    at org.apache.catalina.startup.Catalina.await(Catalina.java:662)
    at org.apache.catalina.startup.Catalina.start(Catalina.java:614)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:585)
    at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289)
    at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)

我在论坛上看到有人遇到了同样的问题。Netstat 不显示端口 8080、8005 或 8009 被占用。我尝试更改端口,但是出现了相同的错误。我还检查了 Windows\System32\drivers\etc 中的 hosts 文件以及该行。

127.0.0.1       localhost

没有被注释掉。我没有改动server.xml文件,但从昨天开始就不能工作了。 有什么想法吗?

编辑:

<Server port="8005" shutdown="SHUTDOWN">

Connectors:
 <Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000" 
               redirectPort="8443" />

<Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol" SSLEnabled="true"
               maxThreads="150" scheme="https" secure="true"
               clientAuth="false" sslProtocol="TLS"
               />

<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
感谢你的帮助。

这不是一个关闭问题,而是一个启动问题。可能是Tomcat已经在运行了。 - user207421
请发布所有<Connector>元素和<Server>元素的start-element。 - Christopher Schultz
@rnunes,目前我在本地机器上没有使用Tomcat。 - gajo
9个回答

11
我通过在conf/server.xml文件中指定以下内容来解决此问题:

<Server address="0.0.0.0" port="8005" shutdown="SHUTDOWN">


对我来说,实际上 address="0.0.0.0" 并不总是有效的。我需要用网络接口的 IP 替换 0.0.0.0(但不要使用回环地址 127.0.0.1,而要使用网络卡的 IP)。 - walkeros
这对我来说是有效的,使用的是tomcat-7.0.62macOS Sierra - loretoparisi
无法在Tomcat 6.x中使用(硬编码InetAddress.getByName("localhost"))。我知道我应该升级。 - YudhiWidyatama
在我的环境中,使用 tomcat-8.5.41CentOS Linux release 7.0.1406 (Core) 并不起作用。 - pengchy
它在我的Tomcat 8.5.71上运行成功,谢谢。 - Chintu Karthi

8

你的服务器IP已更改(甚至是你不使用的其他网络卡的IP),你需要进行编辑。

C:\Windows\System32\drivers\etc\hosts

4
最可能的原因是:您本地主机的IP地址在主机文件中配置不正确。 验证主机文件: 运行:system32:drivers:etc:host 用记事本打开此主机文件
将此localhost行保留为主机文件中的第一行。 关闭所有窗口。 从Eclipse中删除服务器并重新配置它。 这应该会有所帮助。

3
在我的情况下,问题出在hosts文件上(Windows)。 实际上,从一开始我就有以下条目... 172.16.xxx.yyy localhost
大约两周前,我们公司进行了物理服务器和网络域拆分活动,更改了所有机器的IP地址。 在这里,我的yyy被更改了。
当我编辑我的IP时,错误消失了。 感谢Saurabh Chande。

2
我成功解决了这个问题。
我在Eclipse中使用本地Tomcat时遇到了同样的问题(它无法启动,因为找不到tomcat.bin)。 我右键单击Eclipse中的Servers选项卡中的Tomcat服务器,然后单击属性。 在常规属性下,有一个“切换位置”按钮。 它以前指向Eclipse中的本地Tomcat,但是通过此按钮,将其更改为实际Tomcat安装所在的C驱动器上的文件夹。 现在Tomcat在Eclipse和本地都可以正常工作。

1
警告:使用提议的解决方案(服务器地址="0.0.0.0")允许任何攻击者远程关闭您的系统。
我建议使用: < Server address="127.0.0.2" port="8005" shutdown="SHUTDOWN" >
127.0.0.2将作为回送地址工作,但不会干扰在本地主机(127.0.0.1)上侦听的任何其他服务。

根据您提出的配置,任何本地攻击者都可以关闭服务器。目前,在大多数情况下,服务器可以通过操作系统信号进行关闭,因此关闭端口可以安全地禁用。 - Piotr P. Karwasz

0

我知道在这个帖子里发表有点晚了,但我想帮助那些寻找答案并且遇到类似问题的人们。由于我的声望不够,我无法发表评论。

正如楼主所说,

我也检查了Windows\System32\drivers\etc中的hosts文件,行

127.0.0.1 localhost

没有被注释掉。我没有改变server.xml中的任何内容,但是从昨天开始就无法使其工作。有什么想法吗?

我想对此发表评论。在我的情况下,下面的行也没有被注释掉。

127.0.0.1 localhost

然而,还有另一行将“localhost”绑定到旧的本地IP地址。

127.0.0.1 localhost 10.10.1.123 localhost

这是因为最近由于路由器重新配置而更改了我的旧本地IP地址,这就是罪魁祸首。因此,注释掉重复的行解决了我的问题。

结论是,检查是否有多个绑定到“localhost”的绑定,除了检查回环IP是否被注释掉。


0

我在服务器上遇到了这个问题,当时hosts文件的权限设置不正确。

运行chmod 644 hosts解决了这个问题。


0

步骤1:

前往C:\Windows\System32\drivers\etc

步骤2:

找到hosts文件并以管理员模式用记事本打开

步骤3:

将本地主机IP替换为127.0.0.1

例如:

# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
#      102.54.94.97     rhino.acme.com          # source server
#       38.25.63.10     x.acme.com              # x client host

# localhost name resolution is handled within DNS itself.
    127.0.0.1       localhost
#   ::1             localhost

步骤4:

保存该文件并运行服务器


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接